The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Call By Value And Call By Reference

Accessing Function

There are two different way a function can be accessed. Eitheir by passing arguments (parameters) to a function or by passing nothing to a function. Function which doesn't accept any parameters are mostly void type.

Accessing function using parameters are of two types. They are

  • Call by value
  • Call by Reference or address

Before getting into the play of call by value and call by reference, one need to know what is actual parametes and formal parameters.

What Is Actual Parameters

Actual parameters are specified in the function call. In simple terms, value or reference passing at the time of function call.

What Is Formal Parameters

Formal parameters are specified in the function declaration and function definition. In simple terms, variables with datatype at the function declaration and definition.

Call By Value

  • Call by value is the method of passing values as the parameters to the function.
  • In this method, the values of actual parameters are passed to the formal arguments.
  • In call by value method, the values of actual parameters are not changed even they are changed in the called function.
  • Because the actual parameters pass the copy of original parameters to the called function.

C program - Call By Value

Let us write a c program to demonstrate how to use call by value?

callbyvalue.c
#include <stdio.h>
int pass(int, int);     //function declaration
int main()
{
//main function definition
int a = 5, b = 10;
printf("The values of a and b in main function before function call : %d %d ", a, b);
pass(a, b);    //function call with passing arguments as values
printf("\nThe values of a and b in main function after function call : %d %d ", a, b);
}
// function definition
int pass(int a, int b)
{
a = 10; b = 5;     //This change will not affect the actual parameters
printf("\nThe values of a and b in called function : %d %d ", a, b);
}
  • The values of a and b in main function before function call : 5 10
  • The values of a and b in called function : 10 5
  • The values of a and b in main function after function call : 5 10

Note:

The program illustrates that, the actual parameters are passed as copy of original parameters to the called function. So, the changes of values inside the called function does not affect the parameters of calling function.

Call by Reference

  • Call by reference is the method of passing variable addresses as the parameters to the function.
  • In call by reference method, changes made in called function will affect the parameters inside the main function.

C program - Call By Reference

Let us write a c program to demonstrate how to use call by value?

callbyreference.c
#include <stdio.h>
int pass(int*, int*);     //function declaration
int main()
{
//main function definition
int a = 5, b = 10;
printf("The values of a and b in main function before function call : %d %d ", a, b);
pass(&a, &b);    //function call with passing arguments as addresses
printf("\nThe values of a and b in main function after function call : %d %d ", a, b);
}
// function definition
int pass(int * a, int *b)
{
*a = 10; *b = 5;     //This change will affect the actual parameters
printf("\nThe values of a and b in called function : %d %d ",*a, *b);
}
  • The values of a and b in main function before function call : 5 10
  • The values of a and b in called function : 10 5
  • The values of a and b in main function after function call : 10 5

Note:

The program illustrates that, changes made in called function affect the parameters inside the main function. So, the changes made in the parameters are permanent.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report