Call By Value And Call By Reference In C Program

C Language Program

A C language program consists of:

  • The Code
  • Variables – Global/Local
  • Memory management using Heap
  • Stack
  • C Standard Library

When a C program is written it consists of a main program which is a set of methods, procedures, or functions. Methods, procedures or functions operates on data structures such as objects, arrays, stacks, variables etc.,

Each of the modules and data structure is referred to by its name without referring to the actual memory address they occupy. Operating system allocates logical address space to each of the modules and data structures called as segments.

Logical Address Space/Random Access Memory is referred to by a segment number rather than by a segment name (Module Name and Data Structure). When the C program is compiled, Compiler automatically creates segments for modules and data structures used.

Each of the segments resides in RAM/Logical Memory. Each time a module or a data structure is accessed, the CPU generates logical addresses. Each logical address is mapped to physical memory.

Operating systems use segment registers to point to the physical memory address location of modules and data structures used. If the memory address location is not valid then the operating system generates memory fault. In case of memory fault the operating system brings module and associated data structure values back to physical memory by accessing harddisk memory location.

This process of accessing physical memory and secondary memory give rise to Call by Value and Call by reference in C language. Call by Value and Call by Reference is a parameter passing mechanism from main ( ) to functions in C language.

The variables used in function definition are known as Formal Arguments. The variables that are used in the main ( ) function of C language are known as Actual Arguments. The number, type and order of actual arguments and formal arguments must match.

Call by Value

In call by reference the actual value of the module and data structure is changed at secondary memory location. In call by reference actual and formal arguments share the same memory location. As the same memory location is used, changes made to formal arguments are reflected back to actual arguments. In C language this is made possible by the use of pointers. When a function wants to return more than one value then a call by reference becomes useful. Call by reference saves memory space.

Example Program:

  • In this program actual argument addresses are passed to the function.
  • In this program formal arguments are declared as pointer variables having appropriate data type.
#include<stdio.h>
void Call_by_v(int, int);
int main( )
{
	int x = 10, y = 20;
 
	printf("Value of initial x  = %d\n", x);
	printf("Value of initial y = %d\n", y);
 
	printf("\n Function Call \n");
 
	Call_by_v(x, y);
 
	printf("\n After function call, values will be\n\n");
 
	printf("Value of final x = %d\n", x);
	printf("Value of final y = %d\n", y);
	return 0;
}

Output:

Value of initial x  = 10
Value of initial y = 20

 Function Call 

 In function value of x is  = 20
 In function Value of y is  = 30

 After function call, values will be

Value of final x = 10
Value of final y = 20
Code Analysis:

The value of x and y in main ( ) and in function Call_by_v(int, int) are different. From main( ) copy of actual argument x and y is passed to function Call_by_v(int, int). Inside function Call_by_v(int, int) value of x and y is changed by assigning new value to them. New value assigned to x and y is not reflected back in main( ) as we are working on the physical memory location of x and y and not on the actual disk address of x and y.

Call by Reference

In call by reference the actual value of the module and data structure is changed at secondary memory location. In call by reference actual and formal arguments share the same memory location. As the same memory location is used, changes made to formal arguments are reflected back to actual arguments. In C language this is made possible by the use of pointers. When a function wants to return more than one value then a call by reference becomes useful. Call by reference saves memory space.

Example Program:

  • In this program actual argument addresses are passed to the function.
  • In this program formal arguments are declared as pointer variables having appropriate data type.

#include
void call_by_ref(int *, int *);

int main()
{
int x = 10, y = 20;

printf("Value of initial x = %d\n", x);
printf("Value of initial y = %d\n", y);

printf("\nFunction Calling\n");

call_by_ref(&amp;x, &amp;y);

printf("\nAfter function call values will be\n\n");

printf("Value of final x = %d\n", x);
printf("Value of final y = %d\n", y);

return 0;
}

void call_by_ref(int *x, int *y)
{
(*x)++;
(*y)++;

printf("\n Value of x in function = %d\n", *x);
printf("Value of y in function = %d\n", *y);
}

Output:

Value of initial x = 10
Value of initial y = 20

Function  Calling

 Value of x in function = 11
Value of y in function = 21

After function call values will be

Value of final x = 11
Value of final y = 21
Code Analysis:

In call by reference, addresses of integer variables declared in main( ) are passed to a function call_by_ref(int *, int *).

To achieve functionality of call by reference formal arguments are declared as a pointer. By declaring formal arguments as pointers their addresses are passed to function call_by_ref(int *, int *). 

When the execution of function  call_by_ref(int *, int *) gets terminated, the control jumps back to main ( ). Inside the main( ) we get same value that are in function call_by_ref(int *, int *).

In call by reference variable addresses stored on secondary memory are accessed. 

Early binding

Call by Value and Call by Reference is also known as early binding. In early binding arguments passed to functions are evaluated before the function is called.

Late binding

Macro Expansion in C language is an example of late binding. It is called late binding as parameter evaluation is delayed until the argument is actually used in the execution of the function.