On this page (13 sections)
- 🎯 Why Does Parameter Passing Matter?
- 1️⃣ Actual and Formal Parameters
- 2️⃣ Call by Value in Action
- 3️⃣ Proof: Two Different Memory Boxes
- 4️⃣ Expressions and Constants as Arguments
- 5️⃣ Getting a Changed Value Back: return
- 6️⃣ Why a swap Function Fails
- 7️⃣ Order of Arguments Matters
- 8️⃣ Functions Calling Functions
- 9️⃣ The Original Stays Safe, Even in a Loop
- ⚠️ Common Mistakes
- ❓ Frequently Asked Questions
- ✅ Key Takeaways
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.
Figure 2: Actual parameters live in the caller. Formal parameters live in the function.
| Actual parameter | Formal parameter | |
|---|---|---|
| Where it appears | In the function call | In the function definition |
| What it can be | A variable, a constant or an expression | Only a variable declaration with a type |
| Memory | The caller’s own variable | A brand new variable made for this call |
| Lifetime | As long as the caller’s block | Only until the function ends |
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);
}
Before call: marks = 40
Inside function: n = 80
After call: marks = 40
Step by step:
marksis created inmain()and holds 40.- The call
showDouble(marks)copies the value 40 into the formal parametern. - Inside the function,
n = n * 2makesnequal to 80. Only the copy changes. - The function ends and
ndisappears. Back inmain(),marksstill 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);
}
main: x lives at 0x7ffcc60c0494, value 25
function: n lives at 0x7ffcc60c047c, value 25
xinmain()andnin 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;
}
square(a) = 36
square(4) = 16
square(a + 1) = 49
square(square(2)) = 16
square(a): the value of the variablea(6) is copied.square(4): the constant 4 is copied.square(a + 1): C first works outa + 1to get 7, then copies 7.square(square(2)): the inner call returns 4, which becomes the argument of the outer call, giving 16.
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;
}
Ignoring the result: score = 72
Storing the result: score = 77
- The first call
addBonus(score);returns 77, but nobody keeps it, soscorestays 72. - The second call
score = addBonus(score);stores the returned 77 back intoscore. - Inside
addBonus, the parametermarkswas changed too, but that was only the copy.
score = 72
marks = 72 ➜ 77
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);
}
Before: x = 10, y = 20
Inside: a = 20, b = 10
After: x = 10, y = 20
Trace table:
| Step | x (main) | y (main) | a (function) | b (function) | temp |
|---|---|---|---|---|---|
| Call: copies made | 10 | 20 | 10 | 20 | – |
temp = a; | 10 | 20 | 10 | 20 | 10 |
a = b; | 10 | 20 | 20 | 20 | 10 |
b = temp; | 10 | 20 | 20 | 10 | 10 |
| Function ends | 10 | 20 | gone | gone | gone |
- Inside the function,
aandbreally are swapped, and the print line proves it. - But
aandbare copies. When the function ends they vanish, andxandywere never touched.
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;
}
subtract(10, 3) = 7
subtract(3, 10) = -7
Interest = 1200.00
subtract(10, 3)puts 10 intoaand 3 intob, so the result is 7.subtract(3, 10)puts 3 intoaand 10 intob, 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.
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);
}
Largest of 14, 9, 27 is 27
Largest of 5, 5, 3 is 5
maxOfThree(14, 9, 27)copies 14, 9 and 27 intoa,bandc.- 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
aandbin 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;
}
Digit sum of 4823 is 17
number is still 4823
Trace table for sumOfDigits(4823):
| Loop pass | n before | n % 10 | sum after | n after |
|---|---|---|---|---|
| 1 | 4823 | 3 | 3 | 482 |
| 2 | 482 | 2 | 5 | 48 |
| 3 | 48 | 8 | 13 | 4 |
| 4 | 4 | 4 | 17 | 0 |
- The function keeps cutting the last digit off its parameter
nuntil it becomes 0. - That would ruin the number if
nwere the original, but it is a copy, sonumberinmain()still prints 4823. - Call by value is what makes it safe to use parameters as ordinary working variables.
⚠️ Common Mistakes
| Mistake | Why it is wrong | Fix |
|---|---|---|
Expecting swap(x, y) to exchange x and y | The function swaps its own copies | Use pointers (later lecture) |
Calling addBonus(score); and expecting score to change | The returned value was thrown away | score = addBonus(score); |
| Mixing up the order of same-typed arguments | The compiler cannot tell the order is wrong | Check the prototype before calling |
| Thinking formal and actual parameters must share a name | Matching is by position, not name | Choose clear names in each place |
| Using a formal parameter after the function ends | It no longer exists | Return the value instead |
| Passing the wrong number of arguments | The call does not match the prototype | Pass 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
returnand store the returned value. - A simple
swap(int a, int b)cannot work. Pointers solve this later.
🎓 Practice questions: (1) Write a functionvoid tryToReset(int n)that setsnto 0, call it frommain()and explain why the variable inmain()does not change. (2) Print the address of a variable inmain()and of its parameter in a function. Are they equal? (3) Writeint cube(int n)and call it with a variable, a constant and the expression2 + 3. (4) RewritetryToResetso thatmain()does get the new value, usingreturn. (5) Trace by hand the callsumOfDigits(907)in a table.
All 29 lessons in this series (open course page)
Part 1 · Getting Started
- Lesson 1: Introduction to C Programming: Overview, Features and Applications
- Lesson 2: The Structure of a C Program: Building Your First Blueprint
- Lesson 3: Identifiers, Keywords and Variables: Naming Things in C Programming
- Lesson 4: Introduction to Algorithms: Thinking Before You Code
- Lesson 5: Introduction to Flowcharts: Drawing the Logic You Already Know
Part 2 · Data Types, Input/Output and Operators
- Lesson 6: Data Types in C: Choosing the Right Box for Every Value
- Lesson 7: Formatted Input and Output in C: Taking Control of printf and scanf
- Lesson 8: Practice Exercise Sheet: Data Types in C
- Lesson 9: Operators and Expressions in C: Assignment, Unary, and Arithmetic
- Lesson 10: Relational, Logical, and Conditional Operators in C
- Lesson 11: Bitwise and Special Operators in C
- Lesson 12: Operator Precedence and Associativity in C
Part 3 · Decisions and Loops
- Lesson 13: Decision and Case Control: if and if-else in C
- Lesson 14: The switch Statement — and switch vs. if-else Ladder
- Lesson 15: The for Loop: Definite Repetition in C
- Lesson 16: while and do-while Loops: Indefinite Repetition in C
- Lesson 17: break and continue in C: Controlling a Loop from the Inside
- Lesson 18: The goto Statement in C: Jumping Around Your Program
Part 4 · Arrays
- Lesson 19: 1-D Arrays in C: Declaration, Address Calculation, Insertion and Deletion
- Lesson 20: Two-Dimensional Arrays in C: Declaration, Initialization and Accessing Elements
- Lesson 21: Matrix Addition and Matrix Multiplication in C
Part 5 · Strings
- Lesson 22: Strings in C: Working with Character Arrays
- Lesson 23: 1-D and 2-D Character Arrays in C: Storing Names and Words
Part 6 · Functions and Scope
- Lesson 24: Functions in C: Declaration and Definition
- Lesson 25: Categories of Functions in C
- Lesson 26: Scope of Variables in C
- Lesson 27: Call by Value in C: How Parameter Passing Works
- Lesson 28: Passing Arrays to Functions in C: Search, Sum and Sort