# Procedures & Parameter Passing

- **Track:** Cambridge IGCSE™ Computer Science (0478 / 0984) (CAMBRIDGE IGCSE 0478)
- **Module:** Section 8: Programming Concepts & File Handling
- **Estimated Duration:** 10 minutes
- **Access:** Open Theory (All-Access for Interactive IDE &amp; Autotests)

---

## Theory & Conceptual Background

A procedure is a named group of statements called to perform a task. Parameters describe the values it needs; arguments are the actual values supplied by a call. A procedure does not return a value to be used in an expression.

```pseudocode
PROCEDURE ShowTotal(Price : INTEGER, Quantity : INTEGER)
    DECLARE Total : INTEGER
    Total <- Price * Quantity
    OUTPUT Total
ENDPROCEDURE
CALL ShowTotal(6, 4)
```

## Worked call and scope

The arguments 6 and 4 become Price and Quantity within this call. Total becomes 24 and is output. Total is local to the procedure: another procedure cannot assume it can access that local variable. A global variable is declared outside subroutines and can be accessible throughout the program; use parameters where practical to make dependencies clearer.

Use CALL for a procedure. A function instead returns a value and can appear in an expression. Reference-passing mechanisms are optional extension, not needed to understand these examples.

## Maintainability practice

A program prints the same three-line heading in five places. Explain why a named procedure makes a later heading change easier and reduces inconsistent copies. Do not introduce a procedure merely to hide one unexplained operation.

## Three parameters and scope

```pseudocode
PROCEDURE PrintTotal(A : INTEGER, B : INTEGER, C : INTEGER)
    DECLARE Total : INTEGER
    Total <- A + B + C
    OUTPUT Total
ENDPROCEDURE
CALL PrintTotal(2, 4, 7)
```

The parameters receive three argument values and the local Total is 13 during this call. A variable declared inside the procedure is local to that procedure. On paper change the procedure to output the average using ROUND, then call it with different arguments. Check that it uses all three parameters.

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: Procedure Invocation and Return Characteristics (Multiple choice, 25 XP)

**Instructions:**
Which statement is TRUE regarding procedures in Cambridge pseudocode?

**Options:**
- **A**: Procedures must always return an integer value using a RETURN statement.
- **B**: Procedures cannot accept parameters.
- **C**: Procedures perform an action without returning a value and must be invoked using the CALL keyword.
- **D**: Procedures can be used directly on the right-hand side of an assignment statement.


**Autotest Expectations:**
- **Correct procedure characteristics identified**: `selection_match` -> Expected: `opt_call`

### Exercise 2: Application: Scope and calls (Fill in blank, 30 XP)

**Instructions:**
Work out each answer before submitting. Value supplied by a call: parameter or argument? Variable declared inside a procedure: local or global? Keyword used to invoke a procedure:


**Starter Code:**
```text
{"prompt":"Application: Scope and calls","template":"1. Value supplied by a call: parameter or argument? [blank_1]\n2. Variable declared inside a procedure: local or global? [blank_2]\n3. Keyword used to invoke a procedure: [blank_3]","blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}],"word_bank":["argument","parameter","local","global","CALL","RETURN"]}
```


### Exercise 3: Independent Practice: Repeated labels (Pseudocode, 30 XP)

**Instructions:**
Write a procedure that receives a string and a repetition count and outputs that string the requested number of times. Read the string and a count from 0 to 4, then call your procedure.

Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.



**Autotest Expectations:**
- **Two copies**: `test_case` -> Expected: `{&quot;inputs&quot;:[&quot;Ready&quot;,2],&quot;output&quot;:&quot;Ready\nReady&quot;}`
- **No copies**: `test_case` -> Expected: `{&quot;inputs&quot;:[&quot;Ready&quot;,0],&quot;output&quot;:&quot;&quot;}`
- **Different label**: `test_case` -> Expected: `{&quot;inputs&quot;:[&quot;Open&quot;,1],&quot;output&quot;:&quot;Open&quot;}`

### Exercise 4: Application: Parameters and scope (Fill in blank, 30 XP)

**Instructions:**
Work out each answer before submitting. PrintTotal(3, 5, 9) adds its three arguments. Output: A variable declared inside this procedure is local or global? The value supplied when calling a procedure is an argument or its declaration?


**Starter Code:**
```text
{"prompt":"Application: Parameters and scope","template":"1. PrintTotal(3, 5, 9) adds its three arguments. Output: [blank_1]\n2. A variable declared inside this procedure is local or global? [blank_2]\n3. The value supplied when calling a procedure is an argument or its declaration? [blank_3]","blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}],"word_bank":["17","0","local","global","argument","parameter"]}
```


