Published on : 3rd Oct 2021

Call by value vs. Call by reference in C programming
Call by value :
When a function is called by value of variables, the process is known as call by value.
Source Code :
#include <stdio.h> int add(int num1, int num2) { return num1 + num2; } int main(int argc, char** argv) { int num1 = 10, num2 = 20; int res = add(num1, num2); // call by value printf("Addition = %d", res); return 0; }
Output :
Addition = 30
Call by reference or call by address :
When a function is called by addresses of variables, the process is known as call by address or call by reference.
Source Code :
#include <stdio.h> void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } int main(int argc, char** argv) { int num1 = 10, num2 = 20; printf("Before call, num1 = %d and num2 = %d\n", num1, num2); swap(&num1, &num2); // call by reference printf("After call, num1 = %d and num2 = %d\n", num1, num2); return 0; }
Output :
Before call, num1 = 10 and num2 = 20 After call, num1 = 20 and num2 = 10