Call by value and call by reference in C

There are two ways to pass value or data to function in C language: call by value and call by reference. Original value is not modified in call by value but it is modified in call by reference.

Let's understand call by value and call by reference in c language one by one

Call by value in C

In call by value, original value is not modified.
In call by value, value being passed to the function is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only. It will not change the value of variable inside the caller method such as main().
Let's try to understand the concept of call by value in c language by the example given below.
#include <stdio.h>  
void change(int num) {  
printf("Before adding value inside function num=%d \n",num);  
num=num+200;  
printf("After adding value inside function num=%d \n", num);  
}    
int main() {  
int x=200;   
printf("Before function call x=%d \n", x);  
change(x);      //passing value in function  
printf("After function call x=%d \n", x);    
return 0;  
} 
 Output:
Before function call x=200
Before adding value inside function num=200
After adding value inside function num=400
After function call x=200

Call by reference in

In call by reference, original value is modified because we pass reference (address).
Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.
Let's try to understand the concept of call by reference in c language by the example given below:
#include <stdio.h>
void C_val(int *number) {
printf("Before adding value inside function number=%d \n",*number);
(*number) += 80;
printf("After adding value inside function number=%d \n", *number);
   }
int main() {
int x=80;
printf("Before function call x=%d \n", x);
C_val(&x);  //passing reference in function
printf("After function call x=%d \n", x);
return 0;
 }
Output:
Before function call x=80
Before adding value inside function num=80
After adding value inside function num=160
After function call x=160

Difference between call by value and call by reference in c


No.Call by valueCall by reference
1A copy of value is passed to the functionAn address of value is passed to the function
2Changes made inside the function is not reflected on other functionsChanges made inside the function is reflected outside the function also
3Actual and formal arguments will be created in different memory locationActual and formal arguments will be created in same memory location

Recursion in C

When function is called within the same function, it is known as recursion in C. The function which calls the same function, is known as recursive function.
A function that calls itself, and doesn't perform any task after function call, is know as tail recursion. In tail recursion, we generally call the same function with return statement. An example of tail recursion is given below.
Let's see a simple example of recursion.

recursionfunction( ) {  
recursionfunction( ); //calling self function  
} 
Let's see an example to print factorial number using tail recursion in C language.
#include <stdio.h>
int factorial (int n)  
  {  
if ( n < 0)  
return -1; /*Wrong value*/  
if (n == 0)  
return 1;    /*Terminating condition*/  
return (n * factorial (n -1));  
   }
void main( ) {  
int fact=0;    
fact = factorial(6);  
printf("\n factorial of 6 is %d",fact);   
   }  
Output:
factorial of 6 is 720