Parameters and Return Values
Passing values in and getting a result back.
5 minute read
Big idea
Parameters pass values into a function. return sends one value back out.
The idea
int add(int a, int b) {
return a + b;
}
int(first) — the return type: this function gives back anint.(int a, int b)— two parameters: inputs the function receives.return a + b;— ends the function and hands the result back to the caller.
When you call add(3, 4), the arguments 3 and 4 are copied into a and b, the body runs, and the call itself becomes the returned value — so int total = add(3, 4); stores 7.
#include <stdio.h>
int square(int n) {
return n * n;
}
int main(void) {
printf("%d\n", square(5));
printf("%d\n", square(12));
int x = square(3) + square(4);
printf("%d\n", x);
return 0;
}output
25
144
25Run and edit it in the app.
Watch it
Parameters are copies
A function gets its own copies of the arguments. Changing a parameter doesn't change the caller's variable:
To get a result out, return it: x = double_it(x); with int double_it(int n) { return n * 2; }. (To let a function change the caller's variable directly, you'll need pointers — coming up.)
return ends the function
Nothing after a return runs. That makes early exits easy:
#include <stdio.h>
int max(int a, int b) {
if (a > b) {
return b;
}
return a;
}
int main(void) {
printf("%d %d\n", max(3, 9), max(8, 2));
return 0;
}output
9 8Run and edit it in the app.
Think of it like
A juice machine. You put oranges in (parameters), it does its work, and one glass of juice comes out (the return value). Your fruit bowl at home is untouched — the machine got its own oranges.
🕷️ Spidey-sense: I hand Ned the evidence, he runs the analysis, and
returns one answer. He never touches the original — it's a copy. Forensics 101.
Key terms
- Parameter — A variable in the function's header that receives a value.
- Argument — The actual value passed in the call.
- Return value — What the function gives back with
return. - Pass by value — C copies arguments into parameters.
Check yourself
int square(int n) { return n * n; } What is square(4) + 1?
If a function changes its parameter, the caller's variable…
What happens after return?