Call by Value and Call by Reference in C
Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is given an alias for the argument passed, and any changes made to the parameter inside the function will affect the argument used in the call. In contrast, call by value means that the argument is copied into the parameter of the called function, and any changes made to the parameter within the function do not affect the argument used in the call.
When a function is called by reference, the memory address of the argument is passed to the function. However, when a function is called by value, a copy of the argument is passed to the function. This difference makes a significant impact on the way a program behaves. For example, if a function is called by reference and the parameter is modified within the function, the changes will be reflected outside the function. On the other hand, if the same function is called by value and the parameter is modified, the changes will not be reflected outside the function.
When we declare a function in the C programming language, we can specify whether the parameters should be passed by reference or by value.
The syntax for declaring a function with reference parameters is as follows:
Type funcName(Type &var1, Type &var2, ...);
The syntax for declaring a function with value parameters is as follows:
Type funcName(Type var1, Type var2, ...);
For example, consider the following function that multiplies two numbers:
// Function declared with reference parameters
int Multiply(int &a, int &b)

Call by Value
In the C programming language, "call by value" is the method of passing arguments to a function in which the function receives a copy of the value of the argument, rather than the original variable. This means that any changes made to the argument within the function have no effect on the original variable outside of the function. In C, all basic data types (e.g. int, float, char) are passed by value by default.
Here's an example of call by value in C:
Example:
#include <stdio.h>
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
int main() {
int a = 5, b = 10;
printf("Before the swap: a = %d, b = %d\n", a, b);
swap(a, b);
printf("After the swap: a = %d, b = %d\n", a, b);
return 0;
}
Output:
Before the swap: a = 5, b = 10
After the swap: a = 5, b = 10
In this example, the function swap takes two integer arguments, x and y, and swaps their values using a temporary variable temp. When we call swap(a, b) in the main function, the values of a and b are copied into x and y, respectively. The swap happens within the function but the original values passed to the swap() aren't changed.
The output of the code will be:
Before the swap: a = 5, b = 10
After the swap: a = 5, b = 10
As the variables passed to the swap are copies of the original variables. So, any change made on the copies doesn't affect the actual variables.
Call by Reference
In C, "call by reference" is the method of passing arguments to a function in which the function receives a reference (or pointer) to the original variable, rather than a copy of its value. This means that any changes made to the argument within the function are reflected in the original variable outside of the function.
To pass an argument by reference, you need to pass the address of the variable using the '&' operator which is called the address of the operator.
Here's an example that demonstrates how call by reference works in C:
Example:
#include <stdio.h>
void swap(int* x, int* y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a = 5, b = 10;
printf("Before the swap: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After the swap: a = %d, b = %d\n", a, b);
return 0;
}
Output:
Before the swap: a = 5, b = 10
After the swap: a = 10, b = 5
In this example, the swap function takes two arguments, which are pointers to integers (int*). The & operator is used in the main function to pass the addresses of a and b as arguments to the swap function. Inside the function, the * operator is used to dereference the pointers and access the values of the variables. This way, x and y point to the original variables a and b respectively, and any changes made to them affect the original variables. So, the output will be:
Before the swap: a = 5, b = 10
After the swap: a = 10, b = 5
In C language, all pointer types are passed by reference by default.