Define and use a function

Published by Patrick Mutisya · 14 days ago

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

  1. Choose a descriptive function name.
  2. Identify the input parameters required.
  3. Write the function body that performs the required operations.
  4. If a result is needed, use a return statement.
  5. Place the function definition before it is first called (or use forward declarations where required).
  6. 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

MethodDescriptionEffect on Original \cdot ariable
Pass‑by‑valueA copy of the argument is passed.Original variable remains unchanged.
Pass‑by‑referenceThe 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

  1. Write a function maxofthree(a, b, c) that returns the greatest of three numbers.
  2. Explain the difference between a function that returns a value and a procedure that does not. Give an example of each.
  3. 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

AspectDefinitionTypical Syntax (Python)
Function nameIdentifier used to call the functiondef my_function(...):
ParametersVariables listed in the definition that receive argumentsdef my_function(param1, param2):
Return valueData sent back to the callerreturn result
Calling a functionExecuting the function with specific argumentsoutput = my_function(arg1, arg2)
ScopeRegion of the program where a variable is accessibleLocal 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.