Chapter 5 (pp. 133–150) of Computer Science I.
Topics
| Section | Page | Content |
|---|---|---|
| Defining & Using Functions | 134 | Signatures, calling, organizing. |
| How Functions Work | 137 | Call by value vs call by reference. |
| Other Issues | 142 | Functions as entities, overloading, variable-argument functions, optional/default parameters. |
Key Ideas
- Call by value vs call by reference — the central abstraction; made concrete with pointers in C and object references in Java.
- Functions as first-class entities foreshadows function pointers (C) and lambdas (Java).
- Generalized Rounding example recurs across language parts.
Examples
Return a value
function max(a, b):
if a > b:
return a
return b
Call by reference
function swap(ref x, ref y):
temp ← x
x ← y
y ← temp
Mutates the caller’s variables; call by value would leave them unchanged.
In Java
static int max(int a, int b) {
return (a > b) ? a : b;
}
Java passes primitives by value, so it cannot swap two ints through
parameters — wrap them in an array or object to emulate call by reference.
Citations
[1] Computer Science I, Ch. 5, pp. 133–150.