Define and use a procedure

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Structured Programming: Define and Use a Procedure

11.3 Structured Programming

Objective

Define a procedure and demonstrate how to use it correctly in a program.

What is a Procedure?

A procedure (also called a sub‑program, sub‑routine or function) is a named block of code that performs a specific task. It can be called from multiple places in a program, which promotes reuse and makes the code easier to read and maintain.

Key Characteristics

  • Has a unique name.
  • May accept parameters (input values).
  • May return a value (if it is a function) or simply perform an action (if it is a procedure).
  • Encapsulates a logical unit of work.

Procedure Syntax (Python‑like Pseudocode)

def procedure_name(parameter1, parameter2, ...):

# body of the procedure

statements

return result # optional – omitted for a pure procedure

Calling a Procedure

To use a procedure, you call it by writing its name followed by arguments in parentheses.

procedure_name(argument1, argument2, ...)

Parameter Passing Methods

MethodHow it worksEffect on original variable
Call‑by‑valueThe value of the argument is copied into the parameter.Original variable remains unchanged.
Call‑by‑referenceThe parameter becomes an alias for the argument variable.Changes to the parameter affect the original variable.

Example: Calculating the Area of a Circle

Below is a simple procedure that calculates the area of a circle given its radius.

def areaofcircle(radius):

pi = 3.14159

area = pi * radius * radius

return area

# Using the procedure

r = 5

circlearea = areaof_circle(r)

print("Area =", circle_area)

Mathematically, the calculation performed is \$A = \pi r^{2}\$ where \$A\$ is the area and \$r\$ is the radius.

Advantages of Using Procedures

  1. Modularity: Breaks a large program into manageable pieces.
  2. Reusability: Same code can be called from different parts of the program or even other programs.
  3. Readability: Meaningful names describe what the code does, making it easier to understand.
  4. Maintainability: Changes need to be made in only one place.

Common Pitfalls

  • Forgetting to pass the correct number or type of arguments.
  • Modifying parameters unintentionally when using call‑by‑reference.
  • Creating procedures that are too large or perform unrelated tasks.
  • Not returning a value when one is expected (or returning when not needed).

Practice Questions

  1. Write a procedure maxofthree(a, b, c) that returns the greatest of three numbers.
  2. Explain the difference between call‑by‑value and call‑by‑reference using a simple example in pseudocode.
  3. Given the following code, what will be printed? Explain why.

    def increment(x):

    x = x + 1

    return x

    num = 10

    increment(num)

    print(num)

  4. Design a procedure to convert a temperature from Celsius to Fahrenheit. Include the formula \$F = \frac{9}{5}C + 32\$ in your answer.

Suggested diagram: Flowchart showing the steps of defining a procedure, passing parameters, executing the body, and returning a result.