Skip to main content
SingaporeComputer ScienceSyllabus dot point

How do functions package code with parameters and return values, and why do they help?

Define and call Python functions with parameters and return values, and explain the benefits of using functions

A focused answer to the O-Level Computing point on Python functions. Defining functions with def, passing parameters, returning values, the difference between a parameter and an argument, and why functions make programs reusable.

Generated by Claude Opus 4.87 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 define and call Python functions that take parameters and return values, and to explain why functions are useful. The central idea is that a function packages a task under a name, so it can be called many times, tested on its own, and changed in one place, which keeps a program short, clear and reliable.

The answer

Defining a function

You define a function with def, a name, and a list of parameters in brackets. The indented block below is the function body:

def greet(name):
    print("Hello", name)

This defines greet but does not run it yet. The function runs only when it is called.

Calling a function and passing arguments

You call a function by writing its name with arguments in brackets:

greet("Mei")        # prints: Hello Mei

The argument "Mei" is passed in and used as the parameter name inside the function.

Parameters versus arguments

  • A parameter is the name in the definition, a placeholder: name in def greet(name).
  • An argument is the actual value supplied at the call: "Mei" in greet("Mei").

Returning a value

A function can send a result back with return. The returned value can be stored or used in an expression:

def add(a, b):
    return a + b

total = add(2, 3)    # total is 5

A function with no return gives back None by default. Use return when the caller needs the result; use a plain print only when the job is to display something.

Why functions help

Functions make programs better because they:

  • Avoid repetition: write the logic once, call it many times.
  • Are reusable: the same function works in many programs.
  • Are testable: you can check a function alone with known inputs.
  • Are easier to maintain: a change is made in one place.

Examples in context

Example 1. A reusable tax calculation. A program defines tax(amount) once and calls it for every sale in a loop. Because the rule lives in one function, a change to the tax rate is made in a single place, and the function can be tested on known amounts before use.

Example 2. Validating input. A valid_age(age) function returns True or False, and the main program calls it wherever an age is entered. The same checked logic guards several inputs without repeating the conditions, and a fix to the rule updates every use at once.

Try this

Q1. State the difference between a parameter and an argument. [2 marks]

  • Cue. A parameter is the placeholder name in the function definition; an argument is the actual value passed when the function is called.

Q2. Write a function double(n) that returns twice its input. [2 marks]

  • Cue. def double(n): return n * 2.

Q3. Give one benefit of writing code as a function rather than repeating it. [2 marks]

  • Cue. It can be reused by calling it many times, so the logic is written and maintained in one place (testability and readability are also valid).

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 `area_of_rectangle(width, height)` that returns the area, then write code that calls it for a rectangle 55 by 33 and prints the result. Label the parameters and the arguments in your answer.
Show worked answer →

Define the function with def, return the calculation, then call it:

def area_of_rectangle(width, height):
    return width * height

result = area_of_rectangle(5, 3)
print(result)

The parameters are width and height (the names in the definition). The arguments are 55 and 33 (the actual values passed in the call). The function returns 1515, so result is 1515 and the program prints 1515.

Markers reward def with two parameters, a return of width * height, a call with the arguments 55 and 33, and correctly labelling parameters versus arguments.

Original5 marks(a) Explain the difference between a function that returns a value and one that only prints. (b) Give two benefits of writing part of a program as a function rather than repeating the code.
Show worked answer →

(a) A function that returns a value sends a result back to the caller, so it can be stored in a variable or used in an expression (for example total = add(2, 3)). A function that only prints displays text on the screen but gives nothing back, so its result cannot be reused; calling it where a value is expected gives None.

(b) Benefits include: the code can be reused by calling it many times without repeating it; it can be tested in isolation with known inputs; it makes the program shorter and easier to read; and a change to the logic is made in one place. Any two of these are acceptable.

Markers reward return giving back a usable value (versus print only displaying), and two valid benefits such as reuse, testability, readability or single-point maintenance.

Related dot points