# Procedures, Functions and Parameters

> Computer Science · CIE A-Level 9618
> Source: https://www.owlsprep.com/study/cie-9618-u11-procedures-functions-and-parameters/

This module covers core modular programming constructs: procedures, functions, and parameter passing. You will learn key distinctions tested in both theory and practical CIE exams, and avoid common coding traps.

**Prerequisites:** [Basic programming structure and syntax](https://www.owlsprep.com/study/cie-9618-u11-basic-programming-structure/); [Variables and data types](https://www.owlsprep.com/study/cie-9618-u11-variables-data-types/)

## Learning objectives

- Distinguish between procedures and functions in modular programming
- Differentiate between formal and actual parameters
- Compare pass by value and pass by reference parameter passing
- Write correct modular code with parameterised subroutines for CIE exams

## Core Distinction: Procedures vs Functions

Modular programming splits large, complex programs into smaller, reusable blocks called subroutines. This makes code easier to test, debug, and reuse across multiple parts of a program. There are two core types of subroutines in CIE A-Level Computer Science.

**Procedure** — A subroutine that performs an action but does not return any value to the calling code.

**Function** — A subroutine that performs a calculation and returns exactly one value to the calling code.

**Worked example:** Write pseudocode for a procedure that prints a greeting, and a function that calculates the area of a circle.

1. First, declare the procedure (no return value):
2. ```
PROCEDURE printGreeting(name: STRING)
    OUTPUT "Hello, " + name + "!"
ENDPROCEDURE
```
3. Next, declare the function (specify return type and return a value):
4. ```
FUNCTION calculateArea(radius: REAL) RETURN REAL
    CONSTANT PI = 3.14159
    area = PI * radius * radius
    RETURN area
ENDFUNCTION
```
5. When called, the procedure directly produces output, while the function returns a value that can be stored: `myArea = calculateArea(5)` stores the result in `myArea`.

> **exam_tip**
>
> Always use the correct keyword in CIE pseudocode: `FUNCTION` only if you return a value, else use `PROCEDURE to avoid losing marks.

## Formal vs Actual Parameters

Parameters are inputs passed to a subroutine to make it reusable for different values. CIE exams regularly test the distinction between formal and actual parameters, which is a common 1-2 mark theory question.

**Formal Parameter** — A placeholder variable declared in the subroutine definition that holds the input while the subroutine runs.

*Example:* In `FUNCTION calculateArea(radius: REAL)`, `radius` is the formal parameter.

**Actual Parameter** — The concrete value or variable passed into the subroutine when it is called.

*Example:* In `myArea = calculateArea(5)`, `5` is the actual parameter.

**Worked example:** Identify formal and actual parameters in: `PROCEDURE addScore(studentID: INTEGER, newScore: INTEGER)` ... `addScore(currentStudent, 95)`

1. Formal parameters are found in the subroutine definition: `studentID` and `newScore`.
2. Actual parameters are passed when calling the subroutine: `currentStudent` (the variable holding the actual ID) and `95` (the literal score value).

## Parameter Passing: By Value vs By Reference

This is one of the most frequently tested concepts in CIE 9618 theory exams, appearing in almost every paper 1. The core difference is how changes to the parameter inside the subroutine affect the original variable.

**Pass by Value** — A copy of the actual parameter's value is created for the subroutine to use. Changes to the formal parameter do not affect the original variable in calling code.

**Pass by Reference** — A reference (memory address) of the original actual parameter is passed. Changes to the formal parameter modify the original variable in calling code.

**Worked example:** Predict the output of the code below for both passing methods:
```
DECLARE y : INTEGER
y = 10
PROCEDURE changeValue(x : INTEGER)
    x = x * 2
ENDPROCEDURE
CALL changeValue(y)
OUTPUT y
```

1. If passed by value: A copy of `y = 10` is assigned to `x`. Changing `x` to 20 only modifies the copy. Output is `10`.
2. If passed by reference: `x` points to the same memory as `y`. Changing `x` to 20 updates the original value. Output is `20`.

> **exam_tip**
>
> Always check the parameter passing method when tracing output — this is almost always the trick in exam questions.

## Return Values and Use Cases

**Check your understanding**

Test your understanding so far:

1. Which of the following is a defining feature of a function?

   - It can never take parameters
   - It always returns a value to calling code
   - It can only use pass by reference
   - It cannot be reused

   *Answer:* It always returns a value to calling code

   *Why:* Correct! Functions are defined by returning a value, unlike procedures.

2. If you modify a pass by value parameter inside a subroutine, what happens to the original variable?

   - The original variable is modified
   - Only the copy of the parameter is modified
   - The program throws an error
   - The actual parameter changes but the formal does not

   *Answer:* Only the copy of the parameter is modified

   *Why:* Correct! Pass by value passes a copy, so only the copy is changed.

**Worked example:** Write a subroutine to swap two integers, what parameter passing do you need?

1. We need to modify the original two variables, so we use a procedure with both parameters passed by reference (marked `REF` in CIE pseudocode):
2. ```
PROCEDURE swap(a: INTEGER REF, b: INTEGER REF)
    temp = a
    a = b
    b = temp
ENDPROCEDURE
```
3. If we passed by value, the original variables would not change, so the swap would not work. If we needed only one value returned, we would use a function instead.

## Common pitfalls

- **Wrong:** Treating a procedure like a function and assigning its output to a variable
  - Why it fails: Procedures do not return values, so this causes syntax/runtime errors and loses marks in practical exams
  - Correct: Use a function if you need to get a value back from the subroutine, only use procedures for actions with no return
- **Wrong:** Confusing pass by value and pass by reference when predicting output
  - Why it fails: Exams intentionally test this distinction, and it is the most common source of lost marks in code tracing questions
  - Correct: Remember: Pass by Value changes the Value (copy), Pass by Reference changes the Original (reference)
- **Wrong:** Mixing up formal and actual parameter names in exam answers
  - Why it fails: CIE mark schemes require correct naming of parameter types to award marks
  - Correct: Remember: Formal = Definition (F = Form = Definition), Actual = Call (A = Actual = Call)
- **Wrong:** Trying to return multiple values from a single CIE pseudocode function
  - Why it fails: CIE A-Level pseudocode only allows one return value per function, so this is not accepted
  - Correct: If you need to modify multiple values, use a procedure with pass by reference parameters instead

## Cheatsheet

| Concept | Key Feature | Exam Note |
| --- | --- | --- |
| Procedure | No return value, performs action | Use `PROCEDURE` keyword, don't assign result |
| Function | Returns one value, for calculations | Always include a `RETURN` statement |
| Formal Parameter | Placeholder in subroutine definition | Found in the subroutine declaration |
| Actual Parameter | Value/variable passed at call | Found when you invoke the subroutine |
| Pass by Value | Copy passed, original unchanged | No side effects on calling variables |
| Pass by Reference | Address passed, original modified | Required for swapping or multiple changes |

## What's next

Procedures, functions, and parameters are the foundation of all modular programming, which is core to both CIE 9618 Paper 1 (theory) and Paper 2 (practical) assessments. Mastering parameter passing is critical for tracing code output in theory questions and writing clean, working programs for your practical exam. These concepts are used constantly in every advanced programming topic you will study next, from recursion to object-oriented programming. Building a solid understanding now will make learning more complex topics much easier.

- [File Handling](https://www.owlsprep.com/study/cie-9618-u11-file-handling/)
- [Object-oriented programming concepts](https://www.owlsprep.com/study/cie-9618-u11-object-oriented-programming-concepts/)
- [Declarative programming concepts](https://www.owlsprep.com/study/cie-9618-u11-declarative-programming-concepts/)

---

From [OwlsPrep](https://www.owlsprep.com) — free study guides for A-Level, IB, AP and IGCSE, written against the official syllabus. Canonical page: https://www.owlsprep.com/study/cie-9618-u11-procedures-functions-and-parameters/
