Arguments passing in C | Call by value | Call by reference

Preview
You must first complete Types of function in C Language before viewing this Lesson

Arguments passing in C

Arguments can be passed to a function in two ways:

  • Call by value
  • Call by reference

Call by Value

It is also termed as passing arguments by value. In this case, the values of actual arguments are copied to formal arguments on one to one basis in function definition and any changes made to the formal arguments in the function body don’t have any effect on the actual arguments in the calling function.

Features

  • In call by value, new memory location is created for formal arguments for use within the function body. The memory is freed once the program control leaves the function.
  • Changes made to the formal arguments are not affected outside the function.
  • Both the actual and formal parameters will be created in different memory locations.
  • Call by value means that programmer sends some value coping from one function to another.
  • Call by value increases the memory assigned to the code because of the copy of arguments is created at each call to the function.
Program to demonstrate the use of call by value
#include<stdio.h>
void show(int a,int b)
{
a++;
b–;
printf(“\n%d,%d”,a,b);
}
int main()
{
int x=10,y=8;
printf(“\n%d,%d”,x,y);
show(x,y);
printf(“\n%d,%d”,x,y);
return(0);
}
Output
10,8
11,7
10,8
Description

In the above program, values of x and y in main() are passed as actual arguments to the function show().

Value of x is copied to a and value of y is copied to b. a is incremented and b is decremented by 1 inside the function.

Then program control is transferred back to the main() after the function calling statement.

On displaying values of x and y, it is seen that changes made to a and b have no effect on the values of x and y in the main() function.



Call by reference

It is also termed as passing arguments by reference. In this case, the addresses of actual arguments are stored in formal arguments in function definition and any changes made to the formal arguments in the function body are reflected back to the calling function and actual arguments will get latest values of formal arguments.

Features

  • In call by value, memory location of actual arguments is used for formal arguments within the function body.
  • Changes made to the formal arguments have direct affect outside the function.
  • Both the actual and formal parameters will be created in same memory locations.
  • Call by reference sends the memory address of the variable to the function so the function works with the original value.
  • There is no increase in memory space as same memory location is used both for actual as well as formal argument.
  • In call by reference, address is passed by using symbol & and the value is accessed by using symbol *.
  • Changes made to formal arguments are reflected to actual arguments. In this way more than one value can be returned the calling function in an indirect way.
Program to demonstrate the use of call by reference
#include<stdio.h>
void show(int *a,int *b)
{
(*a)++;
(*b)–;
printf(“\n%d,%d”,*a,*b);
}
int main()
{
int x=10,y=8;
printf(“\n%d,%d”,x,y);
show(&x,&y);
printf(“\n%d,%d”,x,y);
return(0);
}
Output
10,8
11,7
11,7
Description

In the above program, values of x and y in main() are passed as actual arguments to the function show().

Address of x is stored in a and address of y is stored in b. *a is incremented and *b is decremented by 1 inside the function.

Then program control is transferred back to the main() after the function calling statement.

On displaying values of x and y, it is seen that changes made to a and b have direct effect on the values of x and y in the main() function i.e. value of *a is transferred to x and value of *b is transferred to y.



Difference between Call by value and call by reference

Call by ValueCall by Reference
1. In call by value, actual arguments are copied to formal arguments on one to one basis.1. In call by reference, addresses of actual arguments are stored in formal argument on one to one basis.
2. Changed made to formal arguments have no effect on actual arguments.2. Changes made to formal arguments have direct effect on actual arguments.
3. Actual arguments and formal arguments are stored at different memory locations.3. Actual arguments and formal arguments share same memory locations.
4. Function works with the copy of actual arguments.4. Function works with the original value of actual arguments.
5. No special symbol is required for using call by value.5. Address operators & and indirection operator * are required for implementing call by reference.
6. Only one value can be returned.6. More than one value can be returned by the function.



Passing Array as function argument

Just like normal variables, we can also pass arrays as function arguments. While passing an array as an actual argument, we need not to write any subscript or specify any size with array name.

i. Passing one dimensional array as function argument

In case of one dimensional array, we can simply write the name of one dimensional array within the parenthesis of calling function which is to be passed as actual argument. There is no need to write any subscript or any size with array name.

In the formal argument, we need to write the name of array followed by pair of square brackets. This pair of square brackets may or may not contain any size as the array automatically takes the size depending upon the size of actual argument.

Program to pass a one dimensional array as function argument.
#include<stdio.h>
void show(int a[])
{
int i;
printf(“\nArray elements are: “);
for(i=0;i<4;i++)
printf(“\n%d”,a[i]);
}
int main()
{
int x[4]={4,5,7,3};
show(x);
return(0);
}
Output
Array elements are:
4
5
7
3
Description

In the above program, array x is passed as actual argument to the function show().

Address of array x is stored in array a

All values stored in array x get transferred to array a.

Elements of array are displayed with the help of for statement.



ii. Passing a two dimensional array as function argument

In case of two dimensional array, we can simply write the name of two dimensional array within the parenthesis of calling function which is to be passed as actual argument. There is no need to write any subscript or any size with array name.

In the formal argument, we need to write the name of array followed by two pairs of square brackets. In these pairs, first pair of square brackets may or may not contain any size but second pair must contain some size within the square brackets.

Program to pass a two dimensional array as function argument.
#include<stdio.h>
void show(int a[][2],int m,int n)
{
int i,j;
printf(“\nMatrix within function is: “);
for(i=0;i<m;i++)
 {
     printf(“\n”);
     for(j=0;j<n;j++)
     {
          printf(“\t%d”,a[i][j]);
     }
 }
}
int main()
{
int x[2][2]={5,7,6,87};
show(x,2,2);
return(0);
}
Output
Matrix within function is:
5       7
6       87
Description

In the above program, two dimensional array x is passed as actual argument to the function show().

Address of array x is stored in array a. m gets 2 and n gets 2 from actual arguments.

All values stored in two dimensional array x get transferred to array a.

Elements of array are displayed.

 

Best Books of C





Lesson tags: call by reference in c, call by value in c, one dimensional array as function argument in c, passing argument to a function in c, two dimensional array as function argument in c
Back to: C Programming Language