Naming Work

Every program so far has been one long main. Real programs, and every exam's later questions, are built from functions: named, reusable pieces of work with their own inputs and one output. You have called library functions since lesson one; this lesson is where you start writing your own, and where the shape of int main(void) finally explains itself completely.

Anatomy of a Definition

Read int cube(int n) the way you learned to read main: the leading int is the return type, cube the name, and (int n) the parameter list, one int received under the name n. Inside, return hands a value back and ends the function; a non-void function must return on every path, because falling off the end and using the result is undefined behaviour, one -Wall catches in the obvious cases.

The call cube(value) is an expression with the returned value, usable anywhere an int is: in printf's arguments, in arithmetic, assigned to a variable. The caller's value is the argument; the function's n is the parameter; the argument's value initializes the parameter at each call, vocabulary exams grade precisely.

Note the file's order: cube is defined above main, so the compiler has seen its full shape before the call. In single-file programs this course keeps helpers above main, which is why the example needs nothing more.

Prototypes: Declaring Without Defining

When definition-above-use is inconvenient, several functions calling each other, or code split across files, a prototype declares the shape alone:

int cube(int n);

Definition minus body, plus semicolon, placed near the top of the file. The compiler can then check every call's argument count and types, converting or rejecting as needed, with the definition arriving later. Two ANSI-specific rules with teeth:

  • int cube(); with empty parentheses is not a prototype: it declares "unspecified parameters", switching off all argument checking, the same historical relic that let chapter 1's misspelled printf compile. A no-argument function is declared int f(void), and this is exactly why main is int main(void).
  • Calling a function the compiler has never seen invokes old C's implicit declaration: it assumes the function returns int and takes whatever you passed, unchecked. Modern compilers warn; this course treats every such warning as an error, and prototypes make the whole issue vanish.

void: Nothing In, Nothing Out

A function can return nothing, take nothing, or both:

void as return type means the function acts rather than answers, called as a statement, not inside an expression. A bare return; exits such a function early; reaching the closing brace is fine too. Each function also declares its own locals at the top of its own block, the chapter 2 rule now applied per function: greet's i is invisible to main and vice versa, a separation the next lesson makes precise.

Why Bother: Decomposition

The exam answer for "why functions" lists reuse, readability, and testing, and all three are real, but the working answer is simpler: a function is a named idea. cube(x) states intent where x * x * x states arithmetic; a main that reads as validate, compute, report, each a call, can be understood one idea at a time. From this chapter on, course programs split their work: main orchestrates, helpers compute, and each function stays small enough to read whole. The chapter 15 capstone is this principle at full scale.

Key Takeaways

  • A definition is return type, name, parameter list, body; return produces the value, on every path, or the use is undefined behaviour.
  • Arguments initialize parameters at each call; the call itself is an expression with the returned value.
  • Helpers sit above main in single-file code; otherwise a prototype (definition minus body, plus semicolon) declares the shape first.
  • int f(void) means no parameters; int f() means unspecified and disables checking; never write the latter, and now you know main's full spelling.
  • void return means act-not-answer; call as a statement, exit early with bare return;.
  • Functions are named ideas: main orchestrates, helpers compute.