Functions
Splitting a program into named pieces, passing values in and getting results back.
Why functions
A program written as one long list of statements quickly becomes impossible to follow. Functions let you give a name to a piece of work, write it once and use it wherever it is needed.
A function has four parts.
int add(int a, int b) { // result type, name, parameters
return a + b; // body
}
| Part | In the example | Meaning |
|---|---|---|
| Result type | int | The kind of value it hands back |
| Name | add | What you write to use it |
| Parameters | int a, int b | The inputs, each with a type and a name |
| Body | return a + b; | The statements that do the work |
Calling a function
To use a function, write its name followed by the values for its parameters. Those values are called arguments.
#include <stdio.h>
int square(int x) {
return x * x;
}
int main(void) {
int side = 6;
int area = square(side);
printf("area = %d\n", area);
printf("%d\n", square(3) + square(4));
return 0;
}
A call can be used anywhere a value can. The call runs the function, and the returned value takes its place in the expression.
Parameters are copies
When you call a function, each argument is copied into the matching parameter. The function works on its own copies. Whatever it does to them, the caller's variables are not affected.
#include <stdio.h>
void try_to_change(int value) {
value = 99; // changes only this function's copy
printf("inside: %d\n", value);
}
int main(void) {
int number = 5;
try_to_change(number);
printf("outside: %d\n", number);
return 0;
}
This is called passing by value. A function hands information back through its return value. The last guide in this track shows the other way, pointers.
return
return ends the function immediately and hands back a value. Nothing after it in that function runs.
#include <stdio.h>
int larger(int a, int b) {
if (a > b) {
return a; // leaves the function here
}
return b; // reached only when the if was false
}
int main(void) {
printf("%d\n", larger(8, 3));
printf("%d\n", larger(2, 9));
return 0;
}
A function that hands nothing back is declared void. It can still use a bare return; to leave early.
Functions that answer a question return bool, which holds true or false.
bool is_overheating(int temperature) {
return temperature > 80; // the comparison is already true or false
}
Declare before use
The compiler reads a file from top to bottom. When it meets a call, it must already know that the function exists. So either define the function above the code that calls it, or tell the compiler about it first with a declaration.
A declaration, also called a prototype, is the first line of the function followed by a semicolon.
#include <stdio.h>
int triple(int x); // declaration: "this exists, the body comes later"
int main(void) {
printf("%d\n", triple(7));
return 0;
}
int triple(int x) { // definition
return x * 3;
}
Delete the declaration line and run it again to see the error. This is also what #include <stdio.h> is for. That file contains the declaration of printf.
Where a variable lives
A variable exists only inside the braces where it is declared. This region is called its scope.
- A variable declared inside a function is local. It is created when the function is called and disappears when the function returns. Other functions cannot see it.
- A variable declared outside all functions is global. It exists for the whole run, and every function can read and change it.
#include <stdio.h>
int total_presses = 0; // global: shared by every function
void button_pressed(void) {
int bonus = 1; // local: exists only during this call
total_presses += bonus;
}
int main(void) {
button_pressed();
button_pressed();
button_pressed();
printf("presses: %d\n", total_presses);
return 0;
}
Globals are convenient and risky, because any function anywhere can change them. Keep them few, and prefer passing values in and returning results.
Remembering between calls with static
Sometimes one function needs to remember something between calls, and nothing else should touch it. A local variable marked static does that. It is created once and keeps its value.
#include <stdio.h>
int next_id(void) {
static int id = 0; // set up once, then kept between calls
id++;
return id;
}
int main(void) {
printf("%d ", next_id());
printf("%d ", next_id());
printf("%d\n", next_id());
return 0;
}
Remove the word static and run it again. Every call now starts from 0, so the function always returns 1.
| Kind | Where declared | Lives | Who can use it |
|---|---|---|---|
| Local | Inside a function | During one call | That function |
| Static local | Inside a function, with static | The whole run | That function |
| Global | Outside all functions | The whole run | Every function |
Writing good functions
- One job each. If you need the word "and" to describe a function, it is probably two functions.
- Name it after what it does.
read_temperature,is_valid,set_speed. - Keep it short. A function you can see all at once is a function you can check.
- Build on helpers.
distance(a, b)can callabsolute(a - b)and stay one line long.
Small functions can be tested one at a time. That is exactly what the practice problems do. Each test calls one function and checks one answer.
Common mistakes
- Forgetting
returnin a function that should hand back a value. - Expecting a function to change the variable you passed in.
- Calling a function before the compiler has seen it.
- Using a global where a parameter and a return value would do.
- Printing a result when the caller needed it returned.
Interview question: What is the difference between declaring and defining a function?
Answer: A declaration, or prototype, states the function's name, parameter types and result type and ends with a semicolon. It tells the compiler the function exists. A definition includes the body and is the actual code. There may be many declarations and exactly one definition. Header files hold declarations so that several source files can call the same function.
In the product: an e-bike controller
An e-bike's firmware is split into small functions that each do one job: read the throttle, limit the speed, compute the motor's power. Small functions can be tested on their own, which matters when one bug can throw a rider off.
#include <stdio.h>
// Throttle 0..1023 to power 0..250 W.
int throttle_to_watts(int throttle) {
return throttle * 250 / 1023;
}
// The law caps assistance at 25 km/h: no power above it.
int limit_for_speed(int watts, int speed_kmh) {
if (speed_kmh >= 25) return 0;
return watts;
}
int main(void) {
printf("%d W\n", limit_for_speed(throttle_to_watts(1023), 18));
printf("%d W\n", limit_for_speed(throttle_to_watts(512), 18));
printf("%d W\n", limit_for_speed(throttle_to_watts(1023), 26));
return 0;
}
Build it: the tare button
This is step 4 of the module project. Put a bowl on the scale, press Tare, and the display goes back to 0, so you weigh only what goes into the bowl. The scale has to remember the bowl's weight between button presses.
Lab: Calibrate, tare and remember with static
The load cell gives a raw number, not grams. The factory measured that raw 8400 means an empty scale, and every 21 raw counts are 1 gram.
raw_to_grams(raw)returns(raw - 8400) / 21.tare(raw)remembers the current weight (in grams) as the tare. Keep it in astaticvariable inside the file, so it survives between calls.net_grams(raw)returns the weight minus the tare.tare_clear()sets the tare back to 0.
Going deeper
Interview question: How would you split an e-bike controller's firmware into modules, and which would you test first?
Answer: Inputs (throttle, brake levers, pedal sensor, speed sensor), the assist policy that turns them into a power request, the motor control loop, battery management, and the display and communication. Test the assist policy first: it is pure logic that decides safety-relevant behaviour (cut power on braking, cap at the legal speed), and it can run on a PC with every input combination before any hardware exists.
Interview question: What is the difference between passing by value and passing a pointer in C?
Answer: C always passes arguments by value: the function receives a copy, so changing the parameter does not affect the caller's variable. To let a function modify the caller's variable, pass its address, a pointer, and have the function write through it. Pointers are also used to pass large structs cheaply without copying them, with const when the function only reads.
Interview question: Why should a function be declared before it is used?
Answer: The compiler reads the file from top to bottom and needs to know a function's return type and parameter types to check the call and generate correct code. A prototype, usually in a header, provides that. Without one, older C assumed the function returns int and accepted any arguments, so a mismatch silently passed wrong values; modern compilers treat implicit declarations as errors.
Explain it: In your own words, explain why splitting firmware into small functions is worth it.
Answer: Small functions with clear names make the code read like a description of what it does, so it is easier to understand and review. Each one can be tested on its own, reused in several places instead of copied, and changed without touching the rest. They also limit where variables can be used, which reduces mistakes. The cost of a call is tiny, and the compiler can inline small functions anyway.