Parameters & Return Values
Values in, a result outFunctions become really useful when you can send values in and get a result back. In the lab below you find out what C actually sends (a copy — so a swap function fails!), watch the call stack grow when functions call functions, and see how return can leave a function early.
- Parametera local copy of the argument
- returnone value back to the caller
- Stacklast in, first out
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
Arguments in, a result out
Parameters are the variables in the function head; arguments are the values you write in the call. When the function is called, each argument is worked out and its value is COPIED into the matching parameter, by position. The function works with its copies, and return hands back exactly one value, which replaces the call.
double average(int a, int b) // a, b = parameters
{
return (a + b) / 2.0; // one value comes back
}
average(7, x + 1) // 7 and x + 1 = arguments
// a = 7, b = (value of x + 1) — copies, by position
| Word | Example | Meaning |
|---|---|---|
| Parameter | int a (in the head) | A local variable that receives a value |
| Argument | 7, x + 1 (in the call) | The value sent in; can be any expression |
| Pass by value | swap(x, y) | Only copies are sent — the caller's variables cannot change |
| Return value | return a + b; | Exactly one value, converted to the return type |
| Stack frame | square() on top of main() | Memory for one call: its parameters and local variables |
| Call stack | main → area → square | Frames stacked up; the last one called finishes first |
Passing values
- Every argument is evaluated first: f(x + 1) sends one number.
- The values are copied into the parameters in order: 1st → 1st, 2nd → 2nd.
- The number and types of arguments must match the prototype.
- If the types differ, the value is converted, like an assignment (2 → 2.0).
- The names do not have to match: the parameter a can receive main's x.
Pass by value
- The parameter is a separate variable in the function's own frame.
- Changing the parameter changes only the copy.
- So swap(int a, int b) cannot swap the caller's variables.
- This keeps your variables safe from functions you call.
- To let a function change a variable you pass its address — pointers (Module 30).
Return values and the stack
- return expression; ends the function and sends ONE value back.
- A function may have several return statements; the first one reached wins.
- The returned value is converted to the return type (double area → 12.56).
- Each call pushes a frame; returning pops it — last in, first out.
- A function's local variables disappear with its frame — never return their address.
power and average — parameters, loops and return values
Every line, explained.
#include <stdio.h>
long long power(int base, int exp)
Two parameters; the result can be big, so long long.
{
long long result = 1;
A local variable: lives only during this call.
for (int i = 0; i < exp; i++)
result *= base;
Multiply exp times.
return result;
}
double average(int a, int b)
{
return (a + b) / 2.0;
2.0 makes it a double division.
}
int main(void)
{
int x = 3;
printf("%lld\n", power(x, 4));
base = 3, exp = 4: prints 81
printf("%lld\n", power(2, x + 7));
The argument x + 7 is worked out first (10): prints 1024
printf("%.1f\n", average(7, 8));
Prints 7.5
printf("%lld\n", power(x, 0));
The loop runs 0 times: prints 1
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
Expecting a function to change its argument
void addOne(int n) { n++; }
addOne(x); // x is unchanged
int addOne(int n) { return n + 1; }
x = addOne(x);
n is a copy. Return the new value and store it, or (later) pass a pointer.
Arguments in the wrong order
power(4, 3) // meant 3 to the power 4
power(3, 4) // base first, then exp
Arguments are matched by position, not by name. 4³ = 64 is not 3⁴ = 81.
Ignoring the returned value
square(5); // result lost
int s = square(5);
printf("%d", square(5));
The result has to be used: stored, printed or used in an expression. Otherwise it is thrown away.
Wrong number of arguments
average(7) // average needs 2
average(7, 8)
With a prototype the compiler stops: "too few arguments to function". Every parameter needs a value.
Integer division in the return
double average(int a, int b)
{ return (a + b) / 2; } // 7.0
double average(int a, int b)
{ return (a + b) / 2.0; } // 7.5
The return type does not change the calculation: int / int is done first, then 7 becomes 7.0.
Check your understanding
Answer each question — you will see the explanation straight away.
In int add(int a, int b), what are a and b?
Variables in the function head are parameters; the values in the call are arguments.
void inc(int n) { n++; } — int x = 5; inc(x); What is x now?
n is a copy of x. Changing n does not change x.
int sub(int a, int b) { return a - b; } — sub(2, 9) gives?
By position: a = 2, b = 9, so 2 - 9 = -7.
main calls f, and f calls g. Which function finishes first?
g is on top of the stack: last in, first out.
How many values can one return statement send back?
Exactly one. More results need pointers or a struct (later modules).
Parameters & Return Values quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.