Define and use procedures and functions, with or without parameters

Resources | Subject Notes | Computer Science

Procedures and Functions

What are Procedures?

A procedure is a sequence of instructions that performs a specific task. It's a set of steps designed to achieve a particular outcome. Think of it as a recipe ÔÇô a series of actions you follow to get a result.

  • Procedures are fundamental building blocks of programs.
  • They can be simple (e.g., printing a message) or complex (e.g., calculating a result).
  • Procedures are often used to avoid repeating the same set of instructions multiple times in a program.

What are Functions?

A function is a named block of code that performs a specific task. It's a more structured and reusable version of a procedure. Functions can accept input values (parameters) and can return a value as a result of their operation.

  • Functions promote code reusability.
  • They improve program organization and readability.
  • Functions can simplify complex programs by breaking them down into smaller, manageable parts.

Defining Procedures and Functions

In programming, procedures and functions are defined using specific keywords and syntax. The exact syntax will depend on the programming language being used (e.g., Python, Pascal, etc.).

Generally, a procedure or function definition includes:

  1. A name for the procedure or function.
  2. Parameters (optional) ÔÇô input values the procedure/function might need.
  3. A block of code containing the instructions.

Using Procedures and Functions with Parameters

Parameters are variables that are passed to a function or procedure when it is called. They allow the procedure/function to work with different data each time it is executed. There are two main types of parameters:

  • Input Parameters: These are values passed into the procedure/function to be used within the code.
  • Output Parameters: (Less common in introductory IGCSE) These are variables that the procedure/function can modify and return values through.

Example (Conceptual - Procedure with Parameter)

Consider a procedure that calculates the area of a rectangle. It would take the length and width as parameters.

Parameter Name Purpose
length The length of the rectangle.
width The width of the rectangle.

The procedure would then use these parameters to calculate the area: Area = length * width.

Example (Conceptual - Function with Parameter and Return Value)

Consider a function that adds two numbers. It would take two numbers as parameters and return their sum.

Parameter Name Purpose
number1 The first number to add.
number2 The second number to add.

The function would perform the addition: sum = number1 + number2 and then return the value of sum.

Benefits of Using Procedures and Functions

  • Code Reusability: Avoid writing the same code multiple times.
  • Modularity: Break down complex programs into smaller, more manageable parts.
  • Readability: Make programs easier to understand.
  • Maintainability: Easier to modify and debug programs.