Skip to main content
SingaporeComputer ScienceSyllabus dot point

How do selection, iteration and functions structure a Python program, and why do functions matter?

Use Python selection, iteration and functions with parameters and return values to structure a solution, applying scope correctly

A focused answer to the H2 Computing outcome on Python control flow. Selection with if and elif, iteration with for and while, defining functions with parameters and return values, and local versus global scope.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

What this dot point is asking

SEAB wants you to structure a Python solution with selection, iteration and functions that take parameters and return values, applying scope rules correctly. The central idea is that control flow decides which statements run and how often, while functions package a task under a name so it can be reused, tested and reasoned about in isolation.

The answer

Selection

Selection chooses between branches with if, elif and else. Conditions are tested top to bottom; the first true branch runs and the rest are skipped:

if temperature > 30:
    print("hot")
elif temperature > 20:
    print("warm")
else:
    print("cool")

Order the conditions carefully when ranges overlap, so each value falls into the intended branch.

Iteration

Iteration repeats statements. A for loop iterates a known sequence; a while loop repeats while a condition holds:

for i in range(5):       # definite: runs 5 times
    print(i)

total = 0
while total < 100:       # indefinite: until condition fails
    total = total + 25

Use for when the number of repetitions is known, while when it depends on a condition. Ensure a while loop changes its condition, or it never ends.

Functions, parameters and return values

A function names a reusable block. Parameters are placeholders in its definition; arguments are the values passed when called. return sends a result back:

def area(width, height):     # parameters width, height
    return width * height    # return value

a = area(4, 5)               # arguments 4 and 5; a is 20

A function that returns a value can be used in expressions; one that only acts (printing, saving) may return nothing.

Scope

Scope is where a name is visible:

  • A local variable, created inside a function, exists only within that function and vanishes when it returns.
  • A global variable, defined at the top level, is visible throughout the module.

A function cannot see another function's locals; referencing one raises a NameError. Keeping data local avoids accidental interference between functions.

Examples in context

Example 1. Validating user input. A registration form loops with while until the user enters a valid age, using selection to reject out-of-range values. Wrapping the check in a valid_age(age) function lets the same validation guard several fields, and the loop variable stays local to the input routine.

Example 2. A reusable calculation. A program computing tax on many salaries defines one tax(salary) function and calls it in a loop over employees. Because the logic lives in one named place, a change to the tax rule is made once, and the function can be tested on known salaries in isolation.

Try this

Q1. State the difference between a for loop and a while loop. [2 marks]

  • Cue. for iterates a known sequence a definite number of times; while repeats indefinitely until its condition becomes false.

Q2. What does a function return if it has no return statement? [1 mark]

  • Cue. In Python it returns None by default.

Q3. Why does referencing another function's local variable cause an error? [2 marks]

  • Cue. Local variables exist only within their own function's scope, so the name is not defined elsewhere, raising a NameError.

Exam-style practice questions

Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Original6 marksWrite a Python function `grade(mark)` that returns 'Distinction' for a mark of 70 or above, 'Pass' for 50 to 69, and 'Fail' below 50. Then write a loop that prints the grade for each mark in a list `marks`.
Show worked answer →

Use selection inside a function with a return value, then iterate the list:

def grade(mark):
    if mark >= 70:
        return "Distinction"
    elif mark >= 50:
        return "Pass"
    else:
        return "Fail"

marks = [82, 55, 40, 67]
for mark in marks:
    print(mark, grade(mark))

The elif chain checks ranges from the top down, so once mark >= 70 is false we know it is below 70 before testing >= 50. Each branch returns, so the function ends at the first matching case.

Markers reward the correct boundary conditions (>= 70, >= 50, else), returning a value rather than printing inside the function, and a loop that calls the function for each mark.

Original5 marks(a) Explain the difference between a parameter and an argument. (b) Explain what local scope means and what happens if a function tries to use a variable defined only inside another function. (c) State one advantage of writing a task as a function rather than inline code.
Show worked answer →

(a) A parameter is the variable named in a function's definition (a placeholder); an argument is the actual value passed in when the function is called. In def f(x) then f(5), x is the parameter and 5 is the argument.

(b) Local scope means a variable created inside a function exists only within that function and is destroyed when it returns. A function cannot see variables local to another function; trying to use one raises a NameError, because that name is not defined in the current scope.

(c) Advantages: a function can be reused (called many times without repeating code), tested in isolation, and given a meaningful name that documents intent, making the program modular and easier to maintain.

Markers reward the parameter-versus-argument distinction, local scope confined to the function with a NameError otherwise, and a valid advantage such as reuse or testability.

Related dot points