Now we will discuss the very important concept in C function that is call by value and call by reference:
  • Call by Value: We pass the parameter name in the function call.
  • Call by Reference: We pass the reference (address) of the parameters in function call.
  • Now we will discuss each of them:

    1. Call by Value

    When we pass parameter name in the function call then we say that that it “call by value” because we are having the variable name which contains the value,

    Example: Write a program to swap two numbers (Call by Value)

    #include<stdio.h>
    void swap_var(int a, int b);
    void main() {
    	int a=9,b=8;
    	printf(“We have a=%d and b=%d”,a,b);
    	swap_var(a,b);
    }
    void swap_var(int a, int b) {
    	int temp;
    	temp=a;
    	a=b;
    	b=temp;
    	printf(“\n We have swapped values.  a=%d and b=%d”,a,b);
    }
    Output:
    We have a=9 and b=8
    We have swapped value. a=8 and b=9
    

    How this function is working

    When we say we are calling by value then we always use variable names. Inside the function the temp variable will act as a mediator to swap values. We will show you the instance of values of variables:
    a=9, b=8
    temp=9;
    a=8;
    b=9;
    
    Show we can understand that eventually the values are swapped. Just follow the steps given in the program to understand the process.

    2. Call by Reference

    When we do not use the variable name instead we use address of the variables. Just see the below example to swap the values of the variables using their address.

    Example: Write a program to swap two numbers (Call by Reference)

    #include<stdio.h>
    void swap_var(int *x, int *y);
    void main() {
    	int a=9,b=8;
    	printf(“We have a=%d and b=%d”,a,b);
    	swap_var(&a,&b);
    	printf(“\n We have swapped values using call by reference.  a=%d and b=%d”,a,b);
    }
    void swap_var(int *x, int *y) {
    	int temp;
    	temp=*x;
    	*x=*y;
    	*y=temp;
    }

    Output

    We have a=9 and b=8
    We have swapped values using call by reference. a=8 and b=9
    

    How this function is working:

    We are passing the address of the variable names, see the line below: swap_var(&a, &b); Let say a and b variable are stored at location 1001 and 1003. So we are passing the address of the variable names as. swap_var(1001,1003);
    We will see the function, swap_var as below:
    We have declared two pointer variables to get the address of the variables [in parameter list of function]:
    int *x and int *y; In these variables the address will be taken of variables a and b. As x=1001 and y=1003

    Next we have declared one temp variable to act as a mediator in swapping process:
    temp=*x, this means the value at the address hold by the pointer variable x. As x is having address 1001 and the value at address 1001 is 9. So temp=9;

    Next we have *x=*y, that is clear per se. The value at x will be assigned by the value at y. That means the value of a will be assigned by value of b. That is a=8;

    Now next line is, *y=temp, it says the value at y will be assigned by the value of temp. Because *y is actually b, so b=temp, i.e. b=9 Finally the values are swapped.


    Note: One difference between call by value and call by reference is that the values are not changed permanently in call by value method. As the values of local variables are changed just in the function we have called. Whereas in the later method values of variables are changed permanently. Because the addresses of variables are passed in function call.

    Quantitative Aptitude
    Reasoning
    Programming
    Interview