Written: August 15, 2026
Call by value copies arguments into a function. Call by reference passes addresses so the function can change the caller’s variables.
This walkthrough keeps the logic small and readable so you can type it, run it, and then change one input at a time to see what happens.
What you will build
You will compare a value-swap that fails to change main’s variables with a pointer-swap that succeeds.
- Write a swapbyvalue(int a, int b) that does not affect callers
- Write swapbyref(int a, int b) that swaps through pointers
- Call both from main and print before/after
- Note that &x passes the address of x
Working C example
include <stdio.h>
void swapbyvalue(int a, int b) {
int t = a; a = b; b = t;
}
void swapbyref(int a, int b) {
int t = a; a = b; b = t;
}
int main(void) {
int x = 3, y = 7;
printf("Start: %d %d\n", x, y);
swapbyvalue(x, y);
printf("After value swap: %d %d\n", x, y);
swapbyref(&x, &y);
printf("After ref swap: %d %d\n", x, y);
return 0;
}
How the logic works
Read the program top to bottom: includes and main first, then the statements that change variables, then the print that proves the result.
If your output looks wrong, print the variables before and after the critical lines. That single habit catches most beginner bugs faster than rewriting the whole file.
If a function must update the caller’s int, pass int . If it only needs the number, pass int.
Keep learning
If this walkthrough on Introduction to Pointers in C: Call by Value vs Call by Reference helped, open the code again and change one input or assumption. Small experiments beat rereading the same example.
Want more step-by-step tutorials like this? Browse blog.xqa.io — and tell us which topic you want next.