Procedures, functions and structured programming
| English | Chinese | Pinyin |
|---|---|---|
| structured programming | 结构化编程 | jié gòu huà biān chéng |
| subroutines | 子程序 | zi chéng xù |
| procedure | 过程 | guò chéng |
| function | 函数 | hán shù |
| parameter | 参数 | cān shù |
| argument | 实参 | shí cān |
| pass by value | 传值 | chuán zhí |
| pass by reference | 传引用 | chuán yǐn yòng |
| local | 局部 | jú bù |
| global | 全局 | quán jú |
Breaking a program into pieces
- Structured programming 结构化编程 builds a program from small named subroutines 子程序, each with one job.
- This is decomposition in code — easier to read, reuse and test.
- The key distinction: a procedure 过程 does an action; a function 函数 returns a value.
Procedures vs functions
PROCEDURE Greet(name : STRING)
OUTPUT "Hello, ", name
ENDPROCEDURE
CALL Greet("Ada")
FUNCTION Square(x : INTEGER) RETURNS INTEGER
RETURN x * x
ENDFUNCTION
result ← Square(5) + 1 // 26
- A procedure performs an action and returns nothing; a function returns a value used in an expression.

A programmer writing and testing code.
The call stack: push on call, pop on return
Calling a subroutine pushes a new frame on top; returning pops it and hands a value back to the caller. The call that is running is always the frame on top.
The key difference between a procedure and a function is that a function:
A function returns a value (used in an expression); a procedure performs an action and returns nothing.
A function Square(x) returns x * x. What does the call Square(5) return?
5 × 5 = 25 — the value the function hands back to its caller (the frame popped off the call stack).
Parameters 参数
- A parameter is the variable a subroutine declares; the argument 实参 is the value the caller supplies.
- Pass by value 传值 — the routine gets a copy; changes inside don't affect the caller (use for read-only inputs).
- Pass by reference 传引用 (
BYREF) — the routine gets a reference; changes do affect the caller (use to update a value, e.g.Swap).

Pass by value gives the routine a copy; pass by reference lets it change the caller's variable
Match each term to what it means.
Function vs procedure = returns a value or not; by value vs by reference = copy or original.
Scope and when to use a subroutine
- A local 局部 variable exists only inside its subroutine; a global 全局 is visible everywhere. Prefer locals + parameters — globals make code hard to follow and test.

A global variable is visible everywhere; a local variable exists only inside its own subroutine
- Use a subroutine when logic appears in more than one place, a block has a clear named purpose, or you want to test a piece in isolation.
A local variable exists only inside the subroutine where it is declared, while a global variable is visible everywhere in the program.
Keeping variables local limits their scope, avoids name clashes, and makes a subroutine testable on its own — globals are best avoided.
A good reason to write a subroutine is that:
Subroutines remove duplication, give a named purpose, and can be tested in isolation.
Writing efficient pseudocode
- Move invariants out of loops — compute a value that doesn't change once, before the loop.
- Exit a loop early when the answer is found (stop a linear search at the first match).
- Use meaningful names (
numberOfPupils, notn) and initialise variables before use. - A procedure/function header names the routine and its parameters; the procedure/function interface (header plus return type) is what a caller needs; a function passes back a return value.
To make a loop more efficient, a value that does not change with the loop counter should be:
Hoisting a loop invariant out avoids recomputing the same value on every iteration.
You've got it
- a procedure does an action (no return); a function returns a value
- pass by value = a copy (caller unchanged); pass by reference = changes affect the caller
- prefer local variables + parameters over globals
- efficiency: hoist invariants out of loops, exit early, use meaningful names