Programmer Coding

Call by value/Call by Reference in C Language

Function Arguments?

If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.The formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit. 

There are two ways in which arguments or parameters can be passed to the called function.

1.  Function call by value

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.By default, C programming language uses call by value method to pass arguments. Ingeneral, this means that code within a function cannot alter the arguments used to call thefunction. Consider the function swap() definition as follows.

Syntax

return_type function_name(parameter_type parameter_name) {
// Function body
}

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;

}

/* function definition to swap the values */

void swap(int x, int y) {

int temp;

temp = x;

/* save the value of x */

x = y;

/* put the value of y into x */

y = temp;

/* put the value of x into y */

}

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

2. Function call by reference

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. To pass the value by reference, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.

Syntax

return_type function_name(parameter_type *parameter_name) {
// Function body
}

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.

* &a indicates pointer to a, i.e., address of variable a, and

* &b indicates pointer to b, i.e., address of variable b.

*/

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 *x, int *y) {

int temp;

temp = *x; /* save the value at address x */

*x = *y;   /* put the value at address y into x */

*y = temp; /* put the value at address x into y */

}

Output

Before swap, value of a : 100

Before swap, value of b : 200

After swap, value of a : 200

After swap, value of b : 100

Difference between Call by Value and Call by Reference

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top