Call stack Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

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
Words you need for function calls
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

  1. Every argument is evaluated first: f(x + 1) sends one number.
  2. The values are copied into the parameters in order: 1st → 1st, 2nd → 2nd.
  3. The number and types of arguments must match the prototype.
  4. If the types differ, the value is converted, like an assignment (2 → 2.0).
  5. The names do not have to match: the parameter a can receive main's x.

Pass by value

  1. The parameter is a separate variable in the function's own frame.
  2. Changing the parameter changes only the copy.
  3. So swap(int a, int b) cannot swap the caller's variables.
  4. This keeps your variables safe from functions you call.
  5. To let a function change a variable you pass its address — pointers (Module 30).

Return values and the stack

  1. return expression; ends the function and sends ONE value back.
  2. A function may have several return statements; the first one reached wins.
  3. The returned value is converted to the return type (double area → 12.56).
  4. Each call pushes a frame; returning pops it — last in, first out.
  5. A function's local variables disappear with its frame — never return their address.
Walkthrough

power and average — parameters, loops and return values

Every line, explained.

1 #include <stdio.h>
2
3 long long power(int base, int exp) Two parameters; the result can be big, so long long.
4 {
5 long long result = 1; A local variable: lives only during this call.
6 for (int i = 0; i < exp; i++)
7 result *= base; Multiply exp times.
8 return result;
9 }
10
11 double average(int a, int b)
12 {
13 return (a + b) / 2.0; 2.0 makes it a double division.
14 }
15
16 int main(void)
17 {
18 int x = 3;
19 printf("%lld\n", power(x, 4)); base = 3, exp = 4: prints 81
20 printf("%lld\n", power(2, x + 7)); The argument x + 7 is worked out first (10): prints 1024
21 printf("%.1f\n", average(7, 8)); Prints 7.5
22 printf("%lld\n", power(x, 0)); The loop runs 0 times: prints 1
23 return 0;
24 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

Expecting a function to change its argument

✗ Wrong
void addOne(int n) { n++; }
addOne(x);   // x is unchanged
✓ Right
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

✗ Wrong
power(4, 3)   // meant 3 to the power 4
✓ Right
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

✗ Wrong
square(5);           // result lost
✓ Right
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

✗ Wrong
average(7)          // average needs 2
✓ Right
average(7, 8)

With a prototype the compiler stops: "too few arguments to function". Every parameter needs a value.

Integer division in the return

✗ Wrong
double average(int a, int b)
{ return (a + b) / 2; }   // 7.0
✓ Right
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.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

In int add(int a, int b), what are a and b?

Question 2

void inc(int n) { n++; } — int x = 5; inc(x); What is x now?

Question 3

int sub(int a, int b) { return a - b; } — sub(2, 9) gives?

Question 4

main calls f, and f calls g. Which function finishes first?

Question 5

How many values can one return statement send back?

Assessment

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.