Chapter 7 Key Takeaways: Procedures and Functions

Use this page for quick review and spaced repetition. Revisit it 1 day, 3 days, 1 week, and 2 weeks after completing the chapter.


Core Concepts

  • Procedures are named blocks of code that perform a specific task. They are declared before the main begin...end block and called by name.

  • Functions are like procedures but return a value. A function call is an expression and can appear anywhere a value of the appropriate type is expected.

  • Procedural decomposition (top-down design) is the process of breaking a complex problem into small, named, self-contained subprograms. The main body reads like an outline; each procedure is a self-contained chapter.

Parameter Modes

Mode Keyword Effect Use When...
Value (none) Caller's variable is copied; changes affect only the copy Small types, local modification needed
Variable var Procedure gets an alias to the caller's variable; changes affect the original Procedure must modify caller's data (e.g., reading input)
Constant const Read-only access; compiler prevents modification Read-only parameters, especially strings and large types

Function Return Values

  • Classic style: Assign to the function name: FunctionName := value;
  • Modern style (preferred): Assign to Result: Result := value;
  • Using Result avoids confusion with recursive calls.

Scope and Nesting

  • Local variables are declared inside a procedure and exist only while it executes. They prevent naming conflicts between procedures.
  • Nested subprograms are declared inside other subprograms. They can access the enclosing scope's variables. Limit nesting to one or two levels.

Forward Declarations

  • When two procedures call each other (mutual reference), use a forward declaration to break the circular dependency.
  • Syntax: Write the procedure heading followed by the keyword forward;, then provide the full definition later.

Best Practices

  1. Default to const for read-only parameters, especially strings and records.
  2. Use var only when the procedure must modify the caller's data.
  3. Prefer functions for computations — they should be pure (no side effects).
  4. One task per subprogram. If you cannot name it with a single verb phrase, it is doing too much.
  5. Name procedures with verbs (ReadInput, PrintReport), functions with nouns/adjectives (Average, IsValid).
  6. Declare variables locally — avoid global variables when a local will do.
  7. Keep the main body short — 5 to 15 lines of procedure/function calls that read like an outline.

Common Errors to Avoid

  • Using end. (period) instead of end; (semicolon) after a procedure — this ends the entire program.
  • Forgetting var when a procedure needs to modify the caller's variable.
  • Passing a literal (like 42) to a var parameter — the compiler requires a variable.
  • Forgetting to assign a return value in a function (no Result := statement).
  • Declaring a procedure after code that calls it without using a forward declaration.

PennyWise Progress

  • Version 0.7: PennyWise decomposed into ShowMenu, AddExpense, DisplayExpenses, CalculateTotal, and DisplaySummary. The main body is a simple menu loop. Each procedure uses appropriate parameter modes: var for data modification, const for read-only access.

Key Vocabulary

Term Definition
Procedure A named subprogram that performs an action
Function A named subprogram that computes and returns a value
Parameter A variable in the subprogram heading that receives data from the caller
Argument The actual value or variable passed to a parameter when calling a subprogram
Value parameter A parameter that receives a copy of the argument (default mode)
Variable parameter A var parameter that aliases the caller's variable
Constant parameter A const parameter that is read-only
Forward declaration A heading-only declaration (forward) that promises a full definition later
Procedural decomposition Breaking a problem into named subprograms, each handling one task
Pure function A function with no side effects — same input always produces same output
DRY "Don't Repeat Yourself" — the principle of avoiding duplicated code
Local variable A variable declared inside a subprogram, visible only within it
Nested subprogram A procedure/function declared inside another procedure/function