Use the terminology associated with procedures and functions

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Structured Programming

11.3 Structured Programming

Objective

Use the terminology associated with procedures and functions.

Key Terminology

  • Procedure (or Sub‑routine) – a block of code that performs a task but does not return a value.
  • Function – a block of code that performs a task and returns a single value to the caller.
  • Call (or Invocation) – the act of executing a procedure or function from another part of the program.
  • Return – the statement that ends a procedure/function and optionally supplies a value (for functions).
  • Parameter – a variable listed in the definition of a procedure/function (also called a formal parameter).
  • Argument (or Actual Parameter) – the value or variable supplied to a procedure/function when it is called.
  • Pass‑by‑Value – a method of argument passing where a copy of the argument’s value is given to the called routine.
  • Pass‑by‑Reference – a method of argument passing where the called routine receives a reference (address) to the original data, allowing it to be modified.
  • Local \cdot ariable – a variable declared inside a procedure/function; its scope is limited to that routine.
  • Global \cdot ariable – a variable declared outside any routine; it is accessible from any part of the program.
  • Scope – the region of the program where a variable name is recognised.
  • Recursion – a function or procedure that calls itself directly or indirectly.
  • Modularity – the design principle of breaking a program into independent, reusable procedures/functions.

Procedures vs. Functions

AspectProcedureFunction
PurposePerform an action or series of actions.Compute and return a value.
Return valueNone (may use output parameters).Exactly one value (or a structured value).
Typical useInput/Output, updating data structures, printing.Mathematical calculations, data conversion.
Invocation syntax (pseudo‑code)CALL ProcedureName(arg1, arg2)result ← FunctionName(arg1, arg2)
Effect on callerMay modify global variables or arguments passed by reference.Does not modify caller’s state unless arguments are passed by reference.

Parameter Passing Mechanisms

  1. Pass‑by‑Value

    The called routine receives a copy of the argument. Changes to the parameter do not affect the original variable.

  2. Pass‑by‑Reference

    The called routine receives a reference (address) to the original variable. Modifications affect the caller’s variable.

  3. Pass‑by‑Value‑Result (Copy‑In/Copy‑Out)

    A hybrid: a copy of the argument is passed in, and on return the final value is copied back to the original variable.

Scope and Lifetime

Understanding scope helps avoid naming conflicts and unintended side‑effects.

  • Local scope – variables exist only while the routine is executing.
  • Global scope – variables exist for the entire execution of the program.

Recursion Example

The classic factorial function demonstrates recursion.

function factorial(n: integer) returns integer

if n = 0 then

return 1

else

return n * factorial(n - 1)

end if

end function

Mathematically, this is expressed as:

\$\$\text{factorial}(n) =

\begin{cases}

1, & n = 0 \\

n \times \text{factorial}(n-1), & n > 0

\end{cases}\$\$

Suggested diagram: Flowchart showing a function call stack for the recursive factorial example.

Benefits of Structured Programming

  • Improved readability and maintainability.
  • Easier debugging through isolated modules.
  • Facilitates code reuse via procedures and functions.
  • Supports top‑down design and stepwise refinement.

Common Mistakes to Avoid

  • Confusing procedures with functions – remember only functions return a value.
  • Using global variables excessively – it reduces modularity.
  • Incorrect parameter passing mode – choose pass‑by‑value when the routine must not modify the argument.
  • Missing return statements in functions – leads to undefined behaviour.
  • Infinite recursion – ensure a base case is reachable.