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

You must first complete Introduction to function in C Language before viewing this Lesson

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

Arguments can be passed to a function in two ways:

  • Call by value
  • Call by reference

Call by Value in C Language

In this method, values of actual arguments get copied to formal arguments one to one basis.  Changes made to formal arguments have no effect on the actual arguments.

Program to demonstrate the use of call by value in C Language

#include<stdio.h>
void display(int a,int b)
{
  a--;
  b++;
  printf("\n%d,%d",a,b);
}
int main()
{
  int p=10,q=8;
  printf("\n%d,%d",p,q);
  display(p,q);
  printf("\n%d,%d",p,q);
  return(0);
}
Output
10,8
9,9
10,8
Description
Values of variables p and q are passed as actual arguments to the function display().
Value of p is copied to a and value of q is copied to b. a is decremented and b is incremented by 1 inside the function.
On displaying values of p and q after function call statement, it is seen that changes made to a and b have no effect on the values of p and q .

Call by reference in C Language

In this method, addresses of actual arguments are stored in formal arguments. Changes made to the formal arguments have direct effect on actual arguments.  In this method, address is passed by using symbol & and the value is accessed by using symbol *.

Program to demonstrate the use of call by reference in C Language

#include<stdio.h>
void display(int *a,int *b)
{
  (*a)--;
  (*b)++;
  printf("\n%d,%d",a,b);
}
int main()
{
  int p=10,q=8;
  printf("\n%d,%d",p,q);
  display(&p,&q);
  printf("\n%d,%d",p,q);
  return(0);
}
Output
10,8
9,9
9,9
Description
In the above program, values of p and q are passed as actual arguments to the function display().
Address of variable p is stored in a and address of q is stored in b.
*a is incremented and *b is decremented by 1 .
On displaying values of p and q after function call statement, it is seen that changes made to a and b have direct effect on the values of p and q i.e. value of *a is transferred to p and value of *b is transferred to q.

Difference between Call by value and call by reference in C Language

Call by Value Call by Reference
1. Actual arguments are copied to formal arguments one to one basis. 1. 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. Only one value can be returned from function. 3. More than one value can be returned by the function.
4. Actual arguments and formal arguments are stored at different memory locations. 4. Actual arguments and formal arguments share same memory locations.

Passing Array as function argument in C Language

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 C Language

In case of one dimensional array, we need to 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 while passing it as actual argument but in formal argument, we need to write pair of square brackets.

Program to pass a one dimensional array as function argument in C Language.

#include<stdio.h>
void display(int a[])
{
  int i;
  printf("\nArray elements are: ");
  for(i=0;i<4;i++)
    printf("\n%d",a[i]);
}
int main()
{
  int x[4]={1,3,5,7};
  display(x);
  return(0);
}
Output
Array elements are:
1
3
5
7
Description
In the above program, array x is passed as actual argument to the function display().
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 C Language

In case of two dimensional array, we need to write the name of two dimensional array which is to be passed as actual argument. There is no need to write any subscript or any size with array name while passing it as actual argument. whereas  in the formal argument, we need to write the name of array followed by two pairs of square brackets. first pair of square brackets may or may not contain any size but second pair must contain some size .

Program to pass a two dimensional array as function argument in C language.

#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 P[2][2]={5,7,9,11};
  show(P,2,2);
  return(0);
}
Output
Matrix within function is:
5       7
9       11
Description
In the above program, two dimensional array P is passed as actual argument to the function show().
Address of array P is stored in array a. m gets 2 and n gets 2 from actual arguments.
All values stored in two dimensional array P get transferred to array a.
Elements of array are displayed.

 

Spread the love
Lesson tags: call by reference in c, call by value in c, Difference between Call by value and call by reference, Methods to pass arguments to a function in C Language, 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
Spread the love