Call by Value and Call by Reference in C: Swapping & Pointers | Unit 3 | BKNMU BCA
BKNMU Junagadh | BCA Sem 1 | Unit 3
Call by Value vs Call by Reference in C Programming
Complete Guide to Parameter Passing Mechanisms, Memory Isolation, Pointer Referencing, and Swapping Logic
main(), making it one of the most critical exam topics for BKNMU BCA Semester 1 (Unit 3).
The mechanics of argument evaluation are categorized into four structural components:
1. Value Copying
Passes a duplicate copy of data to formal parameters. Operations in the function do not affect original variables in caller scope.
2. Address Passing
Passes memory addresses (&) to pointer formal parameters (*). Direct dereferencing permanently mutates original variables.
3. Stack Protection
Call by value allocates distinct, separate stack frames, preventing inadvertent side-effects and ensuring total data isolation.
4. Pointer Side-Effects
Call by reference allows a sub-routine to effectively modify and return multiple variable values simultaneously without return statements.
A complete side-by-side comparison of operational behaviors, syntax differences, and memory effects:
| Comparison Factor | Call by Value | Call by Reference |
|---|---|---|
| Argument Passed | Passes actual value copies of variables (e.g. swap(a, b)). |
Passes memory addresses of variables (e.g. swap(&a, &b)). |
| Parameter Receiving | Formal parameters are standard variables (e.g. int x). |
Formal parameters are pointer variables (e.g. int *x). |
| Original Data Impact | Original variables in caller remain completely unmodified. | Original variables in caller are directly altered via dereference. |
| Memory Allocation | Allocates separate memory locations on stack for parameters. | Shares same memory locations; pointers refer to original cells. |
| Safety & Isolation | High data security; sub-functions cannot corrupt caller state. | Lower isolation; unintended alterations mutate caller state. |
| Default Mechanism | C language operates exclusively by value as default mechanism. | Simulated in C using pointer addresses and dereferencing (*). |
1. Call by Value (Original values will NOT change):
#include <stdio.h>
// Formal parameters x and y are independent copies
void swapByValue(int x, int y) {
int temp = x;
x = y;
y = temp;
printf("Inside swapByValue: x = %d, y = %d\n", x, y);
}
int main() {
int a = 10, b = 20;
printf("Before calling: a = %d, b = %d\n", a, b);
swapByValue(a, b); // Copies of a and b are passed
printf("After calling: a = %d, b = %d (NO CHANGE)\n", a, b);
return 0;
}
2. Call by Reference (Original values WILL change):
#include <stdio.h>
// Formal parameters are pointers holding addresses
void swapByReference(int *x, int *y) {
int temp = *x;
*x = *y; // Mutating value directly at address x
*y = temp; // Mutating value directly at address y
}
int main() {
int a = 10, b = 20;
printf("Before calling: a = %d, b = %d\n", a, b);
swapByReference(&a, &b); // Addresses of a and b are passed
printf("After calling: a = %d, b = %d (SWAPPED)\n", a, b);
return 0;
}
Step 1: Argument Evaluation
In Call by Value, the CPU evaluates variable contents (e.g. 10). In Call by Reference, the CPU evaluates pointer memory offsets (e.g. 0x7ffd).
Step 2: Stack Allocation & Assignment
A new stack frame is created. Value passing assigns numbers into fresh variables; reference passing binds pointers directly to caller addresses.
Step 3: Stack Teardown on Return
When the called function finishes, its stack frame is destroyed. Value copies vanish entirely, while reference mutations remain permanent in main().
x = y; instead of *x = *y;) will only swap local pointer addresses inside the function without changing actual caller variable contents.
- Call by Value: Passes value duplicate; caller variables remain unmodified.
- Call by Reference: Passes memory address; caller variables are altered.
- Address-of Operator (
&): Used in the calling statement to pass the memory location. - Dereference Operator (
*): Used in the called function header and body to access target values. - Use Case: Use Call by Value for read-only calculations; use Call by Reference when multiple variables must be modified.