Programs Made of Ideas

This chapter changed the shape of everything you will write from now on: main orchestrates, named functions compute, and the rules governing the boundary, copies in, one value out, scopes sealed, are exact enough to reason about. It also settled two debts: main's full spelling, and chapter 2's storage-class table. The recap.

Definitions, Prototypes, Calls

A definition is return type, name, parameter list, body; helpers sit above main in single-file code so the compiler meets shapes before calls, and a prototype (definition minus body, plus semicolon) declares the shape when order or files demand it. int f(void) means no parameters; bare int f() means unspecified and disables checking, which is why main is spelled the way it is. Arguments initialize parameters; the call is an expression with the returned value; and a non-void function must return on every path, or using the result is undefined behaviour.

Pass by Value

Every argument is copied into a fresh parameter; functions never see caller variables, which kills the value-passing swap (unswapped output, "parameters are copies" as the one-line reason) and licenses free scribbling on parameters. The front door out is return-and-assign, with the caller deciding what changes; the back door is passing addresses, scanf's trick since chapter 1, formalized in the pointers chapter. Side effects stay out of argument lists: f(i++, i) is chapter 3's unsequenced UB in call clothing.

Recursion

Base case plus shrinking recursive case; each call holds fresh parameters on its own stack frame, pending calls resolving in reverse; a missing or unreachable base case exhausts the stack. The exam trio, factorial (12! is int's ceiling), digit sum, Fibonacci, plus the honest verdict: linear problems read better as loops, recursion shines where problems nest.

Scope, Lifetime, Storage

Scope is where a name works: block for locals and parameters, file for globals, inner shadowing outer. Lifetime is when storage exists: automatic dies at block exit; static and file-scope storage lives the program long, zero-initialized by default (automatics stay indeterminate). static in a block: initialized once, persists across calls. static at file scope: hidden from other files, extern's opposite. Globals cost whole-program reasoning; the preference order is parameters and returns, then static locals, then globals with cause.

One Program, Whole Chapter

A recursive descent whose static local remembers the largest n any call saw, guarded input, definition above main: every mechanism of the chapter in one small machine. Predict the output for 5, run it, take the quiz.

Looking Ahead

Chapter 10 gives functions something structured to work on: struct, the mechanism that groups related values, a student's name, marks, and roll number, into one typed record, and the arrays of records that every real program keeps.