8.13 - Subprograms
Introduction to sub programs
Sub programs are sections of code that can be defined once and then called multiple times throughout a program. They help organise code, improve readability, and reduce repetition by allowing the same set of instructions to be used in different places without rewriting them.
Key benefits of sub programs
- Code reuse - Instructions stored under a single name can be executed whenever needed by calling that name.
- Structure and readability - Breaking a program into smaller, named parts makes it easier to understand and maintain.
- Efficiency - Reduces the total amount of code written, as built-in or custom sub programs handle common tasks.
- Built-in examples - High-level programming languages include predefined sub programs, such as
max()to find the highest value in a list orstr()to convert a value to a string.
Sub programs come in two main types: procedures and functions. Procedures perform a set of instructions, while functions do the same but also return a value.
Parameters and arguments
Parameters and arguments allow sub programs to work with different data each time they are called, making them flexible and reusable.
Key definitions
- Parameters - Special variables defined in a sub program to receive input values. They have a name and, in some languages, a data type or default value.
- Arguments - The actual values passed to the parameters when calling the sub program.
For example, a parameter might be named name in a procedure definition, and the argument could be "Pablo" when calling it.
Defining and using procedures
Procedures are sub programs that carry out a specific set of instructions but do not return a value. They are useful for tasks like displaying messages or updating data without needing to produce output for further use.
How procedures work
- Procedures can be defined without parameters for simple tasks or with parameters for more flexibility.
- To use a procedure, call it by its name, providing arguments if required.
- Procedures execute their instructions but do not send back a result.
Example without parameters
Here is a basic procedure that prints a welcome message:
Calling welcome() produces:
Example with parameters
This procedure uses a parameter to personalise the message:
Calling betterWelcome("Pablo") (where "Pablo" is the argument) produces:
Defining and using functions
Functions are sub programs similar to procedures but with one key difference: they always return a value. This makes them ideal for calculations or operations where the result needs to be used elsewhere in the program.
How functions work
- Functions can take zero or more parameters.
- They must end with a return statement to send back a value.
- When calling a function, assign the result to a variable or use it in a statement to store the returned value; otherwise, it is lost.
Example function
This function joins two strings with a space:
Calling and using the function:
Output:
computer science
The result is stored in subject for further use.
Sub programs with loops and conditionals
Sub programs can include any programming elements, such as variables, loops, and conditional statements, to solve complex problems. This allows them to handle tasks like calculations or checks within their code.
Example function with loop and conditional
Consider a function that adds integers between two numbers and checks if the total is divisible by 7:
How this function works:
- The loop sums the numbers.
- The conditional uses MOD (modulo operator) to find the remainder.
- The function returns a boolean value (
TrueorFalse).
This shows how sub programs can combine elements for practical tasks.
Local and global variables in sub programs
Variables in sub programs have a scope, which determines where they can be accessed. Scope is either local (limited to the sub program) or global (accessible throughout the program).
Key definitions
- Local variables - Declared inside a sub program and only usable within it. They are invisible outside and do not affect other parts of the program. Using the same name elsewhere does not cause conflicts.
- Global variables - Declared in the main program using a keyword like
global, making them accessible anywhere, including sub programs. They are useful for data that needs to persist but can be hard to track in large programs.
Advantages of local variables
- They prevent unintended changes from other parts of the program.
- They allow reuse of variable names without issues.
Example with global variables
This procedure uses global variables to track position:
Calling the procedure twice:
Output:
The globals retain values between calls, allowing cumulative updates.