You write a function that adds a five-minute break to a session, call it, print the session, and the break is not there. Nothing crashed and the compiler did not complain. This is one of the first real surprises in C, and it comes from a deliberate rule: a function receives copies of its arguments. Understanding that rule, and the related rule about where a name is visible, tells you exactly what a function can change and what it cannot. On Day 5 you will learn the tool, a pointer, that lets a function change its caller's variable on purpose.
Predict and run
Two variables are called minutes here. Predict both lines.
#include <stdio.h>
void show_extra(int minutes) {
minutes += 5;
printf("Inside: %d\n", minutes);
}
int main(void) {
int minutes = 20;
show_extra(minutes);
printf("Caller: %d\n", minutes);
return 0;
}Check your prediction
Inside: 25
Caller: 20The call copied the value 20 into the parameter. minutes += 5 changed that copy, and the copy was discarded when the function returned. Rename the parameter and its uses to value and run again: the output is identical, which shows the shared name never connected the two variables. A void return type, as here, means the function returns no value at all.
Parameters are local variables
A parameter behaves like a local variable that is initialized from the argument at each call. Each call gets fresh parameters. When the function returns, its parameters and its other local variables stop existing. This is called passing by value, and every argument in C is passed this way.
The benefit is safety: a function cannot accidentally damage its caller's variables. If you want the caller to see a new value, return it, and let the caller decide to store it.
#include <stdio.h>
int add_break(int minutes) {
return minutes + 5;
}
int main(void) {
int session = 20;
add_break(session);
printf("Ignored: %d\n", session);
session = add_break(session);
printf("Stored: %d\n", session);
return 0;
}Ignored: 20
Stored: 25The first call computed 25 and then threw it away, because nothing used the result. The second call's result was assigned back. Changing state is always visible at the call site, which makes programs easier to read.
Scope: where a name can be used
A name declared inside a pair of braces is visible only from its declaration to the closing brace of that block. This is its scope. A variable declared inside a loop body cannot be used after the loop, and a variable declared in main cannot be named inside another function.
int main(void) {
for (int i = 0; i < 3; i++) {
int doubled = i * 2;
}
printf("%d\n", doubled);
return 0;
}The compiler rejects the printf with use of undeclared identifier 'doubled'. The variable existed only inside the loop body, freshly created on each iteration. If you need a value after a block ends, declare the variable before the block, as you did with accumulators.
An inner block may declare a name that already exists outside it. The inner name shadows the outer one until its block ends:
#include <stdio.h>
int main(void) {
int total = 10;
{
int total = 99;
printf("Inner: %d\n", total);
}
printf("Outer: %d\n", total);
return 0;
}Inner: 99
Outer: 10This is legal, but it is a reliable source of confusion: an assignment you meant for the outer variable lands on the inner one. Prefer distinct names.
Why not use global variables?
A variable declared outside every function is global: every function in the file can read and change it. That looks like an easy way around copying, and it is the reason to avoid it. When any function might change a value, you can no longer understand a function by reading its parameters and body; you must read every function that might run before it. Tests also become harder, because results depend on hidden state left by earlier calls. Pass what a function needs as parameters and return what it produces.
Practice
Your turn
Write int clamp_minutes(int minutes) that limits a duration to the range 1 through 180 by assigning to its own parameter, then returning it. In main, start with int session = 200; and show that calling the function alone changes nothing, while assigning its result does:
Before: 200
After call only: 200
After assignment: 180
Small: 1The last line comes from clamp_minutes(-4).
#include <stdio.h>
int main(void) {
int session = 200;
// Write your solution here.
return 0;
}Hint 1: the idea
The parameter is a copy, so assigning to it is safe and invisible to the caller. Only the returned value can reach main, and only if main stores it.
Hint 2: a first step
Handle only the upper limit first, end the function with return minutes;, and write the first three printf lines in main. Check that the second line still says 200 and the third says 180. Then add the lower limit and the Small line.
Show one solution
#include <stdio.h>
int clamp_minutes(int minutes) {
if (minutes < 1) {
minutes = 1;
}
if (minutes > 180) {
minutes = 180;
}
return minutes;
}
int main(void) {
int session = 200;
printf("Before: %d\n", session);
clamp_minutes(session);
printf("After call only: %d\n", session);
session = clamp_minutes(session);
printf("After assignment: %d\n", session);
printf("Small: %d\n", clamp_minutes(-4));
return 0;
}Before: 200
After call only: 200
After assignment: 180
Small: 1The function changed its own copy freely, and the caller only saw the result when it assigned it. That separation is what lets you reason about a call by looking at one line. In the next lesson you will write checks that prove a function like this keeps its promises at the boundaries.
Completion is saved in this browser only. It does not measure mastery.