Call by value and call by reference in c programming-Functions in c

 

Call by value and call by reference in c programming
Call by value and call by reference in c programming


Here, we are going to look at the two ways in which we can call functions in c programming.

i.e by call by value and another one is call by reference, the difference between the two methods and some examples.

Call by value:-

  • In call by value method we pass the value of the actual argument to the  formal parameter or temporary parameter to the function we are calling.
  • We cannot change the value of the original variable but change the value of the formal parameter in the function.
  • The memory for the actual and the formal parameters are different, they are two different variables.
  • The actual argument is used in the function call while we are calling a function whereas the formal parameter is used in the function that we called i.e in the fuction definition.

Example program:-

In this example, as you can see we have a variable 'a' initialize to 10 in the main function. And in the add function we have a formal parameter 'b' of type integer. when we execute the program first we 
print the value of a that is 10 and then we call the function  by passing the value of a as argument. When the function is called the value of a is copied on to the formal parameter b in the function definition and the we are printing the value before and after adding 20 to the variable 'b'. And as you can see the output below we are not changing the value of the original variable, we are changing the value of the formal variable inside the function.





#include <stdio.h>

int add20(int b) {
    printf("Before adding 20 in the function definition = %d \n",b);    
    b=b+20;    
    printf("After adding 20 in the function definition = %d \n", b); 
}
 
int main()
{
    int a=10;
    printf("Example on Call by value\n");
    
    printf("Before calling the function = %d\n", a);
    
    add20(a);
    
    printf("After returning from the function call = %d\n", a);
    
    
    return 0;
}

Output:-
Example on Call by value
Before calling the function = 10
Before adding 20 in the function definition = 10 
After adding 20 in the function definition = 30 
After returning from the function call = 10

Call by reference:-

  • In call by reference we pass the address of the actual variable as an argument instead of passing the value.
  • Therefore the actual parameter and the formal parameter will point to the same memory location. We do this by using the pointers in c.
  • Since the address of both the parameters are same, changing the value of the formal parameter in the function definition will result in the change in the value of the actual parameter.

Example program:-

Let us take the same example which we have seen in the call by value. Here instead of passing the value we are passing the address of the variable by using the 'address of(&)' operator and this will pass the address of the actual variable and in the function definition we are using (*) in the formal parameter to access the value stored in that particular address. As you can see the output below if we change the value of the formal parameter in the function definition it will also change the value of the  actual variable.





#include <stdio.h>

int add20(int *b) {
    printf("Before adding 20 in the function definition = %d \n", *b);    
    *b=*b+20;    
    printf("After adding 20 in the function definition = %d \n", *b); 
}
 
int main()
{
    int a=10;
    printf("Example on Call by reference\n");
    
    printf("Before calling the function = %d\n", a);
    
    add20(&a);
    
    printf("After returning from the function call = %d\n", a);
    
    
    return 0;
}

Output:-
Example on Call by reference
Before calling the function = 10
Before adding 20 in the function definition = 10 
After adding 20 in the function definition = 30 
After returning from the function call = 30

Learn more about Functions in c.

Post a Comment

0 Comments