Skip to main content
Be Bitwise

What Are Functions?

Functions & Control Flow · Coding Fundamentals track

6 min read

Part ofSystems Programming Companion, week 4

In this module

Breaking Programs into Pieces

Picture writing out every step of a recipe in full each time you cooked a meal. No shorthand, no "sauté the onions" — you'd describe every motion of your hand, every twist of the stove dial, from scratch, over and over. Programs without functions look exactly like that: repetitive, fragile, and exhausting to read.

A function is a named block of code that does one specific job. Define it once, then call it whenever you need that job done. Functions are the fundamental building block of organised software.

text
function greet(name) {
print("Hello, " + name + "!")
}
greet("Alice") // prints: Hello, Alice!
greet("Bob") // prints: Hello, Bob!

Three lines of calling code, no duplicated greeting logic. Want to tweak the greeting format later? You change it in one spot.

Parameters and Arguments

Functions get genuinely powerful once they take parameters — placeholder variables that let the function chew on different data each time it's called.

text
function add(a, b) {
return a + b
}
result = add(3, 5) // result is 8
result = add(10, 20) // result is 30

a and b are parameters, declared in the function signature. The values 3 and 5 are arguments — the real data passed in at call time. Inside the function body, a gets 3 and b gets 5.

Some functions take no parameters. Others take a small army. The number and types of parameters a function accepts make up its signature: the contract it offers the outside world about what it eats and what it produces.

Try itAdd a third parameter to add() and pass it through.
C (GCC 9.2.0)
Key Concept

Parameters are the variable names declared in a function definition. Arguments are the concrete values handed over when the function is called. Arguments bind to parameters in order. The distinction matters because parameters live as abstract placeholders, while arguments are real values sitting at real memory locations.

Return Values

Most functions produce a result. The return statement sends a value back to the caller and stops the function dead in its tracks at that point.

text
function square(x) {
return x * x
}
area = square(7) // area is 49

A function without a return value can still pull its weight (printing, mutating data, writing to a file); it just doesn't hand a result back. These are sometimes called procedures or void functions.

The return value is what makes functions composable. You can feed the output of one function straight into another:

text
function double(n) {
return n * 2
}
function increment(n) {
return n + 1
}
result = increment(double(5)) // double(5) returns 10, increment(10) returns 11

Stringing function calls together is one of the most powerful ideas in programming. Big computations get assembled out of small, individually testable pieces.

Scope and Local Variables

Variables declared inside a function are local to it. They live only while the function is running and stay invisible to everything else. This is scope.

text
function calculate() {
let temp = 42 // temp only exists inside calculate
return temp * 2
}
let result = calculate() // result is 84
// print(temp) // ERROR: temp is not defined here

Scope earns its keep by keeping functions from stepping on each other. If every variable were global, visible everywhere, then two functions both using a variable named i would silently clobber each other's data. Scope buys isolation, and isolation is what makes large programs survivable.

The Call Stack in Action

Every time you call a function, the runtime carves out a stack frame — a slab of memory on the call stack holding the function's locals, its parameters, and the return address (where to head back to when the function finishes).

When the function returns, its frame gets popped off and execution resumes at the return address. That's the programmer's-eye view of a mechanism the Stack and Heap module takes apart byte by byte later on.

text
function outer() {
let x = 10
let y = inner(x)
print(y)
}
function inner(n) {
return n + 5
}
outer()

When outer calls inner, the call stack looks like this:

text
| inner(n=10) | <-- top of stack
| outer(x=10) |
| main() |

When inner returns 15, its frame disappears and outer picks up with y = 15.

The animator below plays the same push-and-pop with a three-deep chain of its own: main calls foo, which calls bar. Step through it and watch each call push a frame holding the function's locals and a return address, then watch the returns peel them off in reverse order. The last frame pushed is always the first one popped.

Call Stack Animator

1000ms
Pseudocode
1void bar() {
2 int z = 30;
3}
4 
5void foo() {
6 int y = 20;
7 bar();
8}
9 
10int main() {
11 int x = 10;
12 foo();
13 return 0;
14}
Stack (grows downward)
Stack is empty. Press Step or Call to begin.
Step 0 / 6

Why Functions Matter at the Low Level

At the machine level, a function call boils down to a CALL instruction that pushes the return address onto the stack and jumps to the function's code. RET pops that address and jumps back. Wiring the high-level idea of "calling a function" to the low-level reality of stack manipulation is what unlocks debugging, profiling, and understanding why exploits like buffer overflows work.

Functions do more than keep code tidy: the CPU supports them directly through dedicated instructions. Compile C, C++ or Rust and the machine code is full of this same push-call-return dance. Interpreters such as CPython keep a stack of frames in software for your Python functions instead, but the idea is identical, and the interpreter's own machine code is calling and returning the whole time.

That tight coupling between functions and hardware is precisely why nailing the function abstraction matters so much for low-level programming. In the next lesson, we'll see how functions team up with conditionals to build programs that can pick between different paths of execution.