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.

Call by value in C

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.

Let's try to understand the concept of call by value in c language by the example given below:

 #include<stdio.h>
#include<conio.h>
void change(intnum) { 
printf("Before adding value inside function num=%d \n",num); 
num=num+100; 
printf("After adding value inside function num=%d \n", num); 

} 
int main() { 
int x=100; 
clrscr(); 
printf("Before function call x=%d \n", x); 
change(x);//passing value in function
printf("After function call x=%d \n", x); 
getch(); 
return 0; 

} 

				

Output:

Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=100
				 

Call by reference in C

In call by reference, original value is modified because we pass reference (address).

Note: To understand the call by reference, you must have the basic knowledge of pointers.

Let's try to understand the concept of call by reference in c language by the example given below:

#include<stdio.h>
#include<conio.h>

 void change(int *num) { 
printf("Before adding value inside function num=%d \n",*num); 
(*num) += 100; 
printf("After adding value inside function num=%d \n", *num); 
} 
int main() { 
int x=100; 
clrscr(); 
printf("Before function call x=%d \n", x); 
change(&x);//passing reference in function
printf("After function call x=%d \n", x); 
getch(); 
return 0; 

} 

Output:

Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200
S.no Call by Value Call by reference
1 A copy of value is passed to the function An address of value is passed to the function
2 Changes made inside the function is not reflected on other functions Changes made inside the function is reflected outside the function also
3 Actual and formal arguments will be created in different memory location Actual and formal arguments will be created in same memory location