Parameters and Return Values
beginner12 min readLesson 23 of 148
Value semantics: copies in, one value out. Multiple outputs need pointers.
Parameters are copies
void set_to_zero(int x) {
x = 0; // modifies the COPY โ caller unaffected
}
int a = 5;
set_to_zero(a); // a is still 5
C passes by value, always. The function's parameter is a fresh variable initialized with the argument's value. (Output parameters via pointers come in module 11 โ this is the "why".)
One return value
int clamp(int v, int lo, int hi) {
if (v < lo) return lo;
if (v > hi) return hi;
return v;
}
return exits immediately โ it can appear anywhere in the body. A
non-void function that reaches the end of its body without a return hands
the caller an indeterminate value (undefined behavior if the caller uses
it). GCC warns with -Wreturn-type; our harness compiles with warnings shown.
Early returns beat nesting
// prefer this // over this
int f(int x) { int f(int x) {
if (bad(x)) return -1; if (!bad(x)) {
... main path ... ... deep nesting ...
} }
}