C Programming · December 3, 2026

Call by Value in C: How Parameter Passing Works

Module II · Functions · Parameter Passing: Call by Value


🎯 Why Does Parameter Passing Matter?

Imagine you give a friend a photocopy of your notes and she scribbles all over it. Your original notes are untouched. Now imagine you handed over the original notebook instead. Her scribbles would be in your book forever.

When you call a function in C and pass it a variable, C hands over a photocopy. Call by value means the function receives a copy of each argument, so changes made inside the function never touch the original variables. This one rule explains many surprises that beginners meet, including why a simple swap function does not work.

main()
marks = 40
📄 copy
➜
showDouble(n)
n = 40 ➜ 80
nothing
comes back
➜
main()
marks = 40
(unchanged)

Figure 1: The function gets a photocopy of the value. Doubling the copy leaves the original alone.

1️⃣ Actual and Formal Parameters

Two names appear in every function call, so learn them first.

  • Actual parameters (also called arguments) are the values written inside the brackets where the function is called.
  • Formal parameters are the variables written inside the brackets where the function is defined. They receive the copies.
int total = add(x, y);  ← call: x, y are actual parameters
int add(int a, int b) { … }  ← definition: a, b are formal parameters

Figure 2: Actual parameters live in the caller. Formal parameters live in the function.

Actual parameterFormal parameter
Where it appearsIn the function callIn the function definition
What it can beA variable, a constant or an expressionOnly a variable declaration with a type
MemoryThe caller’s own variableA brand new variable made for this call
LifetimeAs long as the caller’s blockOnly until the function ends
📌 Actual parameters are matched to formal parameters by position, from left to right. The first argument goes into the first parameter, the second into the second, and so on. Names do not have to match.

2️⃣ Call by Value in Action

Our first program passes a mark to a function that doubles it, then checks whether the original changed.

Program 1: The function changes its copy only

#include <stdio.h>

void showDouble(int n);

int main(void) {
    int marks = 40;
    printf("Before call: marks = %d\n", marks);
    showDouble(marks);
    printf("After call:  marks = %d\n", marks);
    return 0;
}

void showDouble(int n) {
    n = n * 2;
    printf("Inside function: n = %d\n", n);
}
🖥️ Output:
Before call: marks = 40
Inside function: n = 80
After call: marks = 40

Step by step:

  • marks is created in main() and holds 40.
  • The call showDouble(marks) copies the value 40 into the formal parameter n.
  • Inside the function, n = n * 2 makes n equal to 80. Only the copy changes.
  • The function ends and n disappears. Back in main(), marks still holds 40.

3️⃣ Proof: Two Different Memory Boxes

Are the actual and formal parameters really different variables? We can ask the computer for the address of each one. The & operator gives the address, and %p prints it. Pointers get their own lectures later, so for now just compare the two addresses.

Program 2: The addresses are different

#include <stdio.h>

void showAddress(int n);

int main(void) {
    int x = 25;
    printf("main:     x lives at %p, value %d\n", (void *)&x, x);
    showAddress(x);
    return 0;
}

void showAddress(int n) {
    printf("function: n lives at %p, value %d\n", (void *)&n, n);
}
🖥️ Output:
main: x lives at 0x7ffcc60c0494, value 25
function: n lives at 0x7ffcc60c047c, value 25
  • x in main() and n in the function show different addresses, so they are two separate boxes in memory.
  • Both boxes hold the same value, 25, because the value was copied across.
  • The exact addresses on your computer will differ from the ones above. What matters is that the two addresses are not the same.
main() memory
x
25
address A
⟶ copy ⟶
showAddress() memory
n
25
address B

Figure 3: x and n are two separate boxes. Only the number 25 travels from one to the other.

4️⃣ Expressions and Constants as Arguments

Because the function only needs a value, an actual parameter does not have to be a variable. A constant, an expression or even another function call works, as long as it produces a value of the right type.

Program 3: Many kinds of actual parameters

#include <stdio.h>

int square(int n);

int main(void) {
    int a = 6;
    printf("square(a) = %d\n", square(a));
    printf("square(4) = %d\n", square(4));
    printf("square(a + 1) = %d\n", square(a + 1));
    printf("square(square(2)) = %d\n", square(square(2)));
    return 0;
}

int square(int n) {
    return n * n;
}
🖥️ Output:
square(a) = 36
square(4) = 16
square(a + 1) = 49
square(square(2)) = 16
  • square(a): the value of the variable a (6) is copied.
  • square(4): the constant 4 is copied.
  • square(a + 1): C first works out a + 1 to get 7, then copies 7.
  • square(square(2)): the inner call returns 4, which becomes the argument of the outer call, giving 16.
💡 Tip: If you pass an expression such as a + 1, the variable a itself is not changed. Only the result of the expression is copied.

5️⃣ Getting a Changed Value Back: return

If a function cannot change the caller’s variable, how does the caller get a new result? Through return. The function sends one value back, and the caller must store it.

Program 4: Ignoring versus storing the returned value

#include <stdio.h>

int addBonus(int marks);

int main(void) {
    int score = 72;
    addBonus(score);
    printf("Ignoring the result: score = %d\n", score);
    score = addBonus(score);
    printf("Storing the result:  score = %d\n", score);
    return 0;
}

int addBonus(int marks) {
    marks = marks + 5;
    return marks;
}
🖥️ Output:
Ignoring the result: score = 72
Storing the result: score = 77
  • The first call addBonus(score); returns 77, but nobody keeps it, so score stays 72.
  • The second call score = addBonus(score); stores the returned 77 back into score.
  • Inside addBonus, the parameter marks was changed too, but that was only the copy.
main()
score = 72
① copy 72 ➜
addBonus(marks)
marks = 72 ➜ 77
② return 77 ➜
main()
score = 77
after score = …

Figure 4: The only way back to the caller is the returned value. The caller has to store it.

6️⃣ Why a swap Function Fails

This is the classic puzzle. We want a function that exchanges two numbers. Watch what call by value does to it.

Program 5: A swap that does not swap

#include <stdio.h>

void swapAttempt(int a, int b);

int main(void) {
    int x = 10, y = 20;
    printf("Before: x = %d, y = %d\n", x, y);
    swapAttempt(x, y);
    printf("After:  x = %d, y = %d\n", x, y);
    return 0;
}

void swapAttempt(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
    printf("Inside: a = %d, b = %d\n", a, b);
}
🖥️ Output:
Before: x = 10, y = 20
Inside: a = 20, b = 10
After: x = 10, y = 20

Trace table:

Stepx (main)y (main)a (function)b (function)temp
Call: copies made10201020–
temp = a;1020102010
a = b;1020202010
b = temp;1020201010
Function ends1020gonegonegone
  • Inside the function, a and b really are swapped, and the print line proves it.
  • But a and b are copies. When the function ends they vanish, and x and y were never touched.
🔭 Looking ahead: To make a swap really work, a function needs access to the original variables, not copies. C does that by passing the address of a variable, which is called call by reference using pointers. You will meet it in the Pointers lectures.

7️⃣ Order of Arguments Matters

Because arguments are matched by position, swapping their order changes the meaning whenever the function treats its parameters differently.

Program 6: Same function, arguments in a different order

#include <stdio.h>

int subtract(int a, int b);
float simpleInterest(float principal, float rate, int years);

int main(void) {
    printf("subtract(10, 3) = %d\n", subtract(10, 3));
    printf("subtract(3, 10) = %d\n", subtract(3, 10));
    printf("Interest = %.2f\n", simpleInterest(5000.0f, 8.0f, 3));
    return 0;
}

int subtract(int a, int b) {
    return a - b;
}

float simpleInterest(float principal, float rate, int years) {
    return principal * rate * years / 100;
}
🖥️ Output:
subtract(10, 3) = 7
subtract(3, 10) = -7
Interest = 1200.00
  • subtract(10, 3) puts 10 into a and 3 into b, so the result is 7.
  • subtract(3, 10) puts 3 into a and 10 into b, so the result is -7.
  • For simpleInterest, the call must follow the declared order: principal, rate, years. Mixing them up still compiles but gives a wrong answer.
⚠️ Warning: The compiler checks the number and the types of arguments, not their meaning. If two parameters have the same type, a wrong order will not be reported.

8️⃣ Functions Calling Functions

Every call copies its own arguments, so a chain of calls is simply several rounds of copying. A bigger task can be built from smaller functions.

Program 7: Largest of three using a largest-of-two function

#include <stdio.h>

int maxOfTwo(int a, int b);
int maxOfThree(int a, int b, int c);

int main(void) {
    printf("Largest of 14, 9, 27 is %d\n", maxOfThree(14, 9, 27));
    printf("Largest of 5, 5, 3 is %d\n", maxOfThree(5, 5, 3));
    return 0;
}

int maxOfTwo(int a, int b) {
    return (a > b) ? a : b;
}

int maxOfThree(int a, int b, int c) {
    return maxOfTwo(maxOfTwo(a, b), c);
}
🖥️ Output:
Largest of 14, 9, 27 is 27
Largest of 5, 5, 3 is 5
  • maxOfThree(14, 9, 27) copies 14, 9 and 27 into a, b and c.
  • The inner call maxOfTwo(a, b) receives copies of 14 and 9 and returns 14.
  • The outer call maxOfTwo(14, c) receives copies of 14 and 27 and returns 27.
  • Each function has its own variables, so the names a and b in the two functions never clash.
main()
calls maxOfThree(14, 9, 27)
↓ ↑ returns 27
maxOfThree
a=14, b=9, c=27
↓ ↑ returns 14, then 27
maxOfTwo
(14, 9) then (14, 27)

Figure 5: Calls stack up while they run. Each function has private copies, and they finish in reverse order.

9️⃣ The Original Stays Safe, Even in a Loop

Program 8: A function that uses up its parameter

#include <stdio.h>

int sumOfDigits(int n);

int main(void) {
    int number = 4823;
    printf("Digit sum of %d is %d\n", number, sumOfDigits(number));
    printf("number is still %d\n", number);
    return 0;
}

int sumOfDigits(int n) {
    int sum = 0;
    while (n > 0) {
        sum = sum + n % 10;
        n = n / 10;
    }
    return sum;
}
🖥️ Output:
Digit sum of 4823 is 17
number is still 4823

Trace table for sumOfDigits(4823):

Loop passn beforen % 10sum aftern after
1482333482
24822548
3488134
444170
  • The function keeps cutting the last digit off its parameter n until it becomes 0.
  • That would ruin the number if n were the original, but it is a copy, so number in main() still prints 4823.
  • Call by value is what makes it safe to use parameters as ordinary working variables.

⚠️ Common Mistakes

MistakeWhy it is wrongFix
Expecting swap(x, y) to exchange x and yThe function swaps its own copiesUse pointers (later lecture)
Calling addBonus(score); and expecting score to changeThe returned value was thrown awayscore = addBonus(score);
Mixing up the order of same-typed argumentsThe compiler cannot tell the order is wrongCheck the prototype before calling
Thinking formal and actual parameters must share a nameMatching is by position, not nameChoose clear names in each place
Using a formal parameter after the function endsIt no longer existsReturn the value instead
Passing the wrong number of argumentsThe call does not match the prototypePass exactly one value per parameter

❓ Frequently Asked Questions

Q1. Is C always call by value?
Yes. C only ever copies values. When you want a function to change a caller’s variable, you copy the address of the variable, and the function uses that address.

Q2. Does a bigger value cost more to copy?
Yes, a little. Copying an int is tiny and fast. This is one reason arrays are handled differently, which you will see in the next lecture.

Q3. Can I change a parameter inside the function?
Yes. It is your own copy, so you can use it as a working variable, as sumOfDigits did.

Q4. What if the argument type does not match the parameter type?
C converts the value to the parameter type if it can, for example an int into a float. Converting a larger type into a smaller one may lose information.

✅ Key Takeaways

  • Actual parameters are in the call. Formal parameters are in the definition.
  • C passes arguments by value: each formal parameter receives a copy.
  • Changing a formal parameter never changes the caller’s variable.
  • Arguments are matched to parameters by position, not by name.
  • To get a result back, use return and store the returned value.
  • A simple swap(int a, int b) cannot work. Pointers solve this later.
🎓 Practice questions: (1) Write a function void tryToReset(int n) that sets n to 0, call it from main() and explain why the variable in main() does not change. (2) Print the address of a variable in main() and of its parameter in a function. Are they equal? (3) Write int cube(int n) and call it with a variable, a constant and the expression 2 + 3. (4) Rewrite tryToReset so that main() does get the new value, using return. (5) Trace by hand the call sumOfDigits(907) in a table.
All 29 lessons in this series (open course page)

Part 1 · Getting Started

Part 2 · Data Types, Input/Output and Operators

Part 3 · Decisions and Loops

Part 4 · Arrays

Part 5 · Strings

Part 6 · Functions and Scope

Part 7 · Pointers