Types of Function Calls | Call by value and Call by Reference

In previous Tutorial we learn about what is function and how to use function in c programming.

Let me give small intro of function.

What is function?

A function is a group of tasks used to execute the predefined operations and returns a value. A large program can be divided into small blocks of code that help to understand the logic, debug, and modified it.

Types of function calls

Based on the way we pass arguments to the function, function calls are of two types.

  1. Call by value – sending the values of arguments.
  2. Call by reference – sending the address of arguments

Call by value:

Here the values of the arguments are passed to the function. Consider this example:

int c = sum( 3 , 4 );   =>  Assume x=3 and y=4

If sum is defined as sum(int a, int b), the values 3 and 4 are copied to a and b. Now even if we change a and b, nothing happens to the variables x and y.

This is call by value.

In C, we usually make a 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. 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 . . .

Call by reference:

Here the address of the variable is passed to the function as arguments.

Now since the addresses are passed to the function, the function can now modify the value of a variable in calling function using * and & operators. Example:

void swap(int *x, int *y)
{
int temp;
temp= *x;
*x = *y;
*y = temp;
}

This function is capable of swapping the values passed to it. If a=3 and b=4 before a call to swap(a,b), a=4 and b=3 after calling swap.

int main()
{
int a=3;	// a is 3 and b is 4
int b=4;
swap(a,b)
return 0;	// now a is 4 and b is 3
}

Example Program

#include 
 
/* function declaration */
void swap(int *, int *);
 
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 *a, int *b) {

   int temp;

   temp = *a; /* save the value of a */
   *a = *b;    /* put b into a */
   *b = temp; /* put temp into b */
  
   return;
}

Output

Before swap, value of a : 15
Before swap, value of b : 20
After swap, value of a : 20
After swap, value of b : 15
Press any key to continue . . .

Example Program

#include 
 
/* function declaration */
void swap(int *, int *);
 
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 *a, int *b) {

   int temp;

   temp = *a; /* save the value of a */
   *a = *b;    /* put b into a */
   *b = temp; /* put temp into b */
  
   return;
}

Output

Before swap, value of a : 15
Before swap, value of b : 20
After swap, value of a : 20
After swap, value of b : 15
Press any key to continue . . .

Exercise on Pointer, Call by value & Call by Reference

  1. Write a program to print the address of a variable. Use this address to get the value of this variable.
  2. Write a program having a variable i. Print the address of i. Pass this variable to a function and print its address. Are these addresses same? Why?
  3. Write a program to change the value of a variable to ten times its current value. Write a function and pass the value by reference.
  4. Write a program using a function that calculates the sum and average of two numbers. Use pointers and print the values of sum and average in main().
  5. Write a program to print the value of a variable i by using the “pointer to pointer” type of variable.
  6. Try problem 3 using call by value and verify that it doesn’t change the value of the said variable.

Was this article helpful?
YesNo

Leave a Comment

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