Cambridge A-Level Computer Science 9618 – Structured Programming: Define and Use a Function
11.3 Structured Programming
Objective: Define and use a function
In structured programming, a function (also called a sub‑program or procedure) is a self‑contained block of code that performs a specific task. Functions improve readability, promote reuse, and make debugging easier.
Key Concepts
Definition – The place where the function is created, specifying its name, parameters, and body.
Calling (Invocation) – The point in the main program (or another function) where the function is executed.
Parameters (Arguments) – Values passed into the function; they act as local variables.
Return value – The result that a function sends back to the caller (optional for procedures).
Scope – Variables defined inside a function are local to that function and cannot be accessed outside it.
General Syntax (Python‑style pseudocode)
def function_name(parameter1, parameter2, ...):
# body of the function
result = ... # optional calculation
return result # optional; omit for procedures
Steps to Define and Use a Function
Choose a descriptive function name.
Identify the input parameters required.
Write the function body that performs the required operations.
If a result is needed, use a return statement.
Place the function definition before it is first called (or use forward declarations where required).
Call the function from the main program or another function, providing appropriate arguments.
Example: Calculating the Area of a Circle
Mathematical formula: \$A = \pi r^{2}\$
def circle_area(radius):
pi = 3.14159
area = pi * radius * radius
return area
# Using the function
r = 5
a = circle_area(r)
print("Area =", a)
Parameter Passing Methods
Method
Description
Effect on Original \cdot ariable
Pass‑by‑value
A copy of the argument is passed.
Original variable remains unchanged.
Pass‑by‑reference
The address of the argument is passed.
Changes inside the function affect the original variable.
Common Errors and How to Avoid Them
Missing return statement – If a value is expected but the function does not return one, the caller receives None (or an undefined value).
Incorrect number of arguments – Ensure the number of arguments in the call matches the function definition.
Variable name clashes – Use distinct names for parameters and global variables to avoid unintended shadowing.
Infinite recursion – Every recursive function must have a clear base case.
Practice Questions
Write a function maxofthree(a, b, c) that returns the greatest of three numbers.
Explain the difference between a function that returns a value and a procedure that does not. Give an example of each.
Given the function definition below, what will be printed?
def add_one(x):
x = x + 1
return x
y = 5
print(add_one(y))
print(y)
Summary Table
Aspect
Definition
Typical Syntax (Python)
Function name
Identifier used to call the function
def my_function(...):
Parameters
Variables listed in the definition that receive arguments
def my_function(param1, param2):
Return value
Data sent back to the caller
return result
Calling a function
Executing the function with specific arguments
output = my_function(arg1, arg2)
Scope
Region of the program where a variable is accessible
Local variables exist only inside the function body.
Suggested diagram: Flowchart showing the steps of defining a function, passing parameters, executing the body, and returning a value.