Function call by Value in C



In C, a function can be called from any other function, including itself. There are two ways in which a function can be called − a) call by value and b) call by reference. By default, the call by value mechanism is employed.

We must know some terminologies to understand how the call by value method works.

Formal arguments − A function needs certain data to perform its desired process. When a function is defined, it is assumed that the data values will be provided, in the form of parameter or argument list inside the parenthesis in front of function name. These arguments are the variables of a certain data type.

Actual arguments − When a certain function is to be called, it should be provided with the required number of values of the same type and in same sequence as used in its definition.

Syntax

type function_name(type var1, type var2, . . .)

The argument variables are called the formal arguments. Inside the function’s scope these variables act as its local variables.

Consider the following function −

int add(int x, int y){
   int z = x+y;
   return z;
}

The arguments x and y in the function definition are the formal arguments.

Example

If the add() function is called, as in the code below −

#include <stdio.h>

int add(int x, int y){
   int z = x+y;
   return z;
}

int main(){
   int a=10, b=20;
   int c = add(a,b);
   printf("addition : %d", c);
}

Output

addition :30

The values or variables inside the parenthesis a and b are the actual arguments. They are passed to the function.

The call by value method implies that the values of the actual arguments are copied in the formal argument variables. Hence, x takes the value of a, and b is assigned to y. The local variable z inside add() function stores the addition and is returned. In the main() function, the value returned by main() is assigned to z, which is printed.

Note that a variable in C is a named location in the memory. Hence, variables are created in the memory and each variable is assigned a random memory address by the compiler.

Random Memory Address

Explanation

Let us assume that the variables a, b and c in main() function occupy memory locations 100, 200 and 300 respectively. When add() function is called with a and b as actual arguments, their values are stored in x and y respectively.

The variables x, y and z are the local variables of add() function. In the memory they will be assigned some random location. Let assume that they are created in memory address 1000, 2000 and 3000 respectively.

Since the function is called by copying the value of actual arguments to corresponding formal argument variables, the locations 1000 and 2000 which are the address of x and y, will hold 10 and 20 respectively. The compiler assigns their addition to the third local variable z, and is returned.

As the control comes back to main(), the returned data is assigned to c, which is displayed as the output of the program.

The default function calling mechanism is call by value. It eliminates a function’s potential side effects, making your software simpler to maintain and understand. It is best suited for a function expected to do a certain computation on the argument received and return the result. Here is another example of call by value.

Example

#include <stdio.h>

/* function declaration */
void swap(int x, int y);

int main (){

   /* local variable definition */
   int a = 100;
   int b = 200;

   printf("Before swap, value of a : %d\n", a );
   printf("Before swap, value of b : %d\n", b );

   /* calling a function to swap the values */
   swap(a, b);

   printf("After swap, value of a : %d\n", a );
   printf("After swap, value of b : %d\n", b );

   return 0;
}
void swap(int x, int y){

   int temp;

   temp = x; /* save the value of x */
   x = y;    /* put y into x */
   y = temp; /* put temp into y */

   return;
}

Output

Before swap, value of a : 100
Before swap, value of b : 200
After swap, value of a : 100
After swap, value of b : 200

It shows that there are no changes in the values, though they had been changed inside the function.

Since the values are copied in different local variables of another function, any manipulation doesn’t have any effect on the actual argument variables in the calling function.

However, call by value method is less efficient when we need to pass large objects such as array or a file to another function. Also, in some cases, we may need the actual arguments to be manipulated by another function. In this case, the call by value mechanism is not useful. We have to explore the call by reference mechanism for the purpose.

The call by reference approach involves passing the address of the variable holding the value of actual argument.

You can devise a calling method that is a mix of call by value and call by reference. In this case, some arguments are passed by value and others by reference.

c_loops.htm
Advertisements