Use parameters

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Structured Programming: Use Parameters

11.3 Structured Programming – Use Parameters

In structured programming, a procedure (or function) is a self‑contained block of code that can be called from other parts of a program. Parameters allow data to be passed into and out of these procedures, making code reusable and easier to understand.

Why Use Parameters?

  • Encapsulation – hide implementation details.
  • Reusability – the same procedure can work with different data.
  • Maintainability – changes to a procedure affect only its definition.
  • Clarity – the purpose of a procedure is evident from its parameter list.

Parameter Passing Mechanisms

Two main mechanisms are used in most languages:

  1. Call‑by‑value – a copy of the argument is passed.
  2. Call‑by‑reference – a reference (address) to the original variable is passed.

Call‑by‑Value

When a procedure is called, the value of each argument is copied into a new local variable (the parameter). Modifications to the parameter do not affect the original argument.

Example (Python‑like pseudocode):

def increment(x):

x = x + 1 # modifies only the local copy

return x

a = 5

b = increment(a) # a remains 5, b becomes 6

Call‑by‑Reference

The procedure receives a reference to the original variable. Any change to the parameter directly alters the caller’s variable.

Example (C++‑like pseudocode):

void double \cdot alue(int &n) { // & indicates reference

n = n * 2; // modifies the original variable

}

int a = 4;

double \cdot alue(a); // a becomes 8

Default Parameters

Many languages allow parameters to have default values, which are used if the caller omits that argument.

Example (Python):

def greet(name, greeting="Hello"):

print(greeting + ", " + name + "!")

greet("Alice") # uses default greeting: "Hello, Alice!"

greet("Bob", "Hi") # overrides default: "Hi, Bob!"

Parameter Scope

Parameters are local to the procedure in which they are declared. Their lifetime begins when the procedure is entered and ends when it returns.

Summary Table of Parameter Features

FeatureCall‑by‑ValueCall‑by‑ReferenceDefault Parameters
What is passed?Copy of the argument’s valueReference (address) to the original variableSpecified value used when argument omitted
Effect on caller’s variableUnaffectedModified if parameter is changedNone – only supplies a value
Memory usageExtra copy createdNo extra copy (reference only)Same as normal parameters
Typical language supportPython, Java (primitive types), CC++, Java (objects), Pascal (var parameters)Python, C++, Java (overloaded methods)

Practical Tips for Exams

  • Identify whether a parameter should be passed by value or by reference based on whether the procedure needs to modify the original data.
  • When writing pseudocode, clearly indicate the passing mechanism, e.g., proc foo(a: int, b: int ref).
  • Remember that default parameters must be placed after any required parameters.
  • Use meaningful parameter names to improve readability.

Example: Sorting a List Using a Procedure with Parameters

The following pseudocode demonstrates a simple bubble‑sort procedure that sorts a list passed by reference.

proc bubbleSort(arr: list ref):

n = length(arr)

repeat

swapped = false

for i = 1 to n-1 do

if arr[i] > arr[i+1] then

temp = arr[i]

arr[i] = arr[i+1]

arr[i+1] = temp

swapped = true

end if

end for

n = n - 1

until not swapped

end proc

# Main program

myList = [5, 2, 9, 1, 6]

bubbleSort(myList) # myList is sorted in‑place

Suggested diagram: Flow of control when calling a procedure with parameters (call stack illustration).

Key Take‑aways

  • Parameters enable procedures to operate on external data without hard‑coding values.
  • Choose the appropriate passing mechanism to control whether changes affect the caller.
  • Default parameters simplify calls and reduce code duplication.
  • Always consider scope and lifetime when designing procedures.