Call by Value in C: Usage and Examples
Table of Content:
In a Computer language, there are two ways that arguments can be passed to a subroutine.
In this tutorial we will talk about Call By Value, let's start
Call By Value
By now we are well familiar with how to call functions. But, if you observe carefully, whenever we called a function and passed something to it we have always passed the values of variables to the called function.
The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. Such function calls are called calls by value. By this what we mean is, on calling a function we are passing values of variables to it.
By default, C programming uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function. Consider the following program:
Example Program
#include int sqr(int p); int main(void) { int n=10; printf("Sqrt of %d is %d\n",n, sqr(n)); return 0; } int sqr(int p) { p = p*p; return(p); }
Output
Sqrt of 10 is 100 Press any key to continue . . .
In this example, the value of the argument to sqr( ), 10, is copied into the parameter p. When the assignment p = p*p takes place, only the local variable p is modified. The variable n, used to call sqr( ), still has the value 10. Hence, the output is Sqrt of 10 is 100.
Remember that it is a copy of the value of the argument that is passed into a function. What occurs inside the function has no effect on the variable used in the call.
Remember that code within a function cannot alter the arguments used to call the function. i.e In call by value, original value is not modified.
Another example of Call by Value
Example Program
#include /* function declaration */ void swap(int p, int q); int main () { /* local variable definition */ int a = 15; int b = 20; 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; } /* function definition to swap the values */ void swap(int p, int q) { int temp; temp = p; /* save the value of p */ p = q; /* put q into p */ q = temp; /* put temp into q */ return; }
Output
Let us put the above code in a single C file, compile and execute it, it will produce the following result
Before swap, value of a : 15 Before swap, value of b : 20 After swap, value of a : 15 After swap, value of b : 20 Press any key to continue . . .
It shows that there are no changes in the values, though they had been changed inside the function.
Call by reference in C
In call by reference, original value is modified because we pass reference (address).
Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.
Note: To understand the call by reference, you must have the basic knowledge of pointers. click here to understand Pointer
click here to understand Call by reference