Follow a Function Call and Its Return Value
Follow a call out and back
announce did its work by printing. A calculation helper can instead return a number for its caller to use. The returned value completes the waiting expression; the caller can store it, calculate with it, or print it.
A function call temporarily hands control to another function. The caller is the function making the call; the callee is the one being called. Returning ends that one invocation (one particular call), then resumes the caller at the place waiting for it. It does not restart main.
Here main asks double_points for a result. The argument value 4 is copied into the parameter points; doubled becomes 8. return doubled; sends the value 8 back, so main can finish initializing result.
#include <stdio.h>
int double_points(int points) {
int doubled = points * 2;
return doubled;
}
int main(void) {
int result = double_points(4);
printf("Result: %d\n", result);
return 0;
}
The output is Result: 8. Returning a number does not display it: printf does that. return 0; in main reports successful completion to the environment that ran the program.
A helper can call another helper
This time a workshop bill includes a fee and then gets doubled for two bookings. Read from main, not from the first function definition. The letter comments mark places in the trace below.
#include <stdio.h>
int add_fee(int amount) {
int fee = 2;
amount = amount + fee; /* C */
return amount; /* D */
}
int booking_total(int price) {
int with_fee = add_fee(price); /* B: wait here for a value */
int total = with_fee * 2; /* E */
return total; /* F */
}
int main(void) {
int price = 5;
int bill = booking_total(price); /* A: wait here for a value */
printf("Price: %d, bill: %d\n", price, bill); /* G */
return 0;
}
| Step | Running function | What happens next |
|---|---|---|
| A | main | Copy 5 into booking_total's parameter price. main waits to finish initializing bill. |
| B | booking_total | Copy its price value, 5, into add_fee's parameter amount. Wait to finish initializing with_fee. |
| C | add_fee | Its local fee is 2. Its own amount changes from 5 to 7. Both callers' price variables stay 5. |
| D | add_fee | Return 7 to B. This invocation ends; its parameter and local variable no longer live. |
| E | booking_total | B completes with with_fee = 7. Execution continues with total = 14. |
| F | booking_total | Return 14 to A. This invocation's parameters and locals no longer live. |
| G | main | A completes with bill = 14. Print Price: 5, bill: 14, then return from main. |
Keep track with a call stack
A stack is a last-in, first-out arrangement: the most recently called function finishes before the waiting caller resumes. Each box below is a frame, a record for one invocation. We draw its parameters, local values, and return point so you can follow what is still needed.
Read the snapshots in numbered order. The uppermost frame is running; lower frames are waiting, not finished. At snapshot 3, all three calls are still active. At snapshot 4, add_fee has ended, but main is still waiting for booking_total. A pending initializer is not a usable value: do not read bill or with_fee before its call has supplied the result.
This is a conceptual call stack, not a picture of measured memory addresses. A machine stack is a common implementation, but C does not require every parameter or local variable to occupy a literal stack slot. A compiler may keep values in registers or inline a helper (replace a call with equivalent work). Those optimizations do not change the observable result of this defined program.
A name's scope is not an object's lifetime
Scope tells you where a name can be used in source code. amount can be named inside add_fee, not inside main. The two variables named price belong to different functions; their spelling does not make them one object.
An object is storage holding a value, such as a variable. Lifetime tells you when an object exists during execution. These ordinary local variables have automatic storage duration. While a helper runs, the waiting caller's locals remain alive even though their names are not in scope in the helper. A local in a smaller { ... } block can stop living when that block ends, before the whole function returns. A later call creates fresh parameters and ordinary locals; it does not revive the previous ones. Returning an int copies out a value, so the result survives without keeping the callee's local variable alive.
The call stack is not another name for the heap. Later, allocated-memory lessons will show storage requested explicitly and kept until it is released. None of the helpers here requests that storage.
Before moving on, trace a second call with a starting price of 3 on paper: amount becomes 5, total becomes 10, and the caller's price stays 3. The next exercise asks you to return a calculation of your own.