Procedures and functions

Cambridge IGCSE Computer Science (0478) · lesson 32 of 35 · 25 min

Why Use Procedures and Functions?

Breaking a program into named, reusable blocks improves:

  • Readability: Code is easier to understand.
  • Maintainability: Fix a bug in one place, not many.
  • Reusability: Call the same block multiple times.
  • Testing: Test each block independently.

Procedures vs Functions

Worked Examples

Procedure to display a greeting

PROCEDURE greet(name : STRING)

OUTPUT “Hello, ” & name

ENDPROCEDURE

CALL greet(“Alice”)

Function to calculate area of a rectangle

FUNCTION area(w : REAL, h : REAL) RETURNS REAL

RETURN w * h

ENDFUNCTION

result ← area(5.0, 3.0)

OUTPUT result

Local vs Global Variables

Local variables are declared inside a procedure/function and only exist while it runs. Global variables are declared outside all procedures and accessible everywhere. Best practice: use local variables to avoid unintended side effects.

What is the key difference between a procedure and a function?

  • Procedures are faster
  • Functions return a value; procedures do not
  • Procedures take parameters; functions do not
  • Functions cannot be called

Answer: Functions return a value; procedures do not
A function returns a value using RETURN; a procedure performs an action but returns nothing.

A variable declared inside a function is:

  • Global
  • Local
  • Constant
  • Static
  • Answer: 1

Variables declared inside a function are local — they only exist during that function’s execution.

How do you call a procedure named “printReport” with no parameters?

  • printReport()
  • CALL printReport()
  • RUN printReport
  • EXECUTE printReport

Answer: CALL printReport()
Cambridge pseudocode uses CALL to invoke a procedure.


Previous lesson: Arrays and string handling
Back to Cambridge IGCSE Computer Science (0478)
Next lesson: File handling and exception handling