6.6 - Subroutines: Procedures & Functions
What subroutines are and their purpose
Subroutines are named blocks of code that contain a specific set of instructions. They allow programmers to reuse the same code multiple times without having to write it out again each time. This is especially useful in larger programs, where repeating code could make the program longer and harder to manage.
The difference between procedures and functions
Subroutines can be divided into two main types based on whether they return a value or not. A value is data that the subroutine sends back to the main program after it finishes running.
Procedures
Procedures are subroutines that carry out a task but do not return any value to the main program.
For example, here is a simple procedure in pseudocode that prints a birthday message:
This procedure has no parameters and just outputs a fixed message when called.
Functions
Functions are subroutines that perform a task and then return a value to the main program. The returned value can be used elsewhere in the code, such as storing it in a variable or using it in another calculation.
When you create a function, you must specify what value it returns using a RETURN statement. Always make sure to handle the returned value in your main program, or it will be lost.
For example, here is a function in pseudocode that takes two numbers and returns the larger one:
This function compares the two inputs and returns the maximum value.
How parameters work in subroutines
Parameters are variables that a subroutine uses to receive data from the main program.
Key points about parameters:
- Not all subroutines need parameters – some can run without any extra data.
- When parameters are used, you must provide values for them when calling the subroutine.
Subroutines without parameters
These run the same way every time, using fixed instructions inside the subroutine.
For example, the bday() procedure from earlier has no parameters and always prints the same message.
Subroutines with parameters
These accept input values, which are passed into the subroutine when it is called. The parameters act like placeholders that get filled with actual data.
For example, here is a procedure with one parameter:
The parameter name is replaced with a specific value when the subroutine is called, customising the output.
Calling subroutines and handling returned values
Calling a subroutine means telling the program to run the instructions inside it. You do this by using the subroutine's name in your main code. If the subroutine has parameters, you must provide values for them in brackets after the name.
Calling procedures
Examples of calling procedures:
- Calling
bday()would output: Happy birthday to you - Calling
bdayPerson("Alex")would output: Happy birthday to Alex
You can call the same procedure multiple times in a program to repeat the task without rewriting the code.
Calling functions and using returned values
Functions are called in the same way, but you need to capture the returned value. This is often done by assigning it to a variable.
For example, using the maxVal function:
How this works:
- This calls the function with parameters 9 and 2.
- The function returns 9 (the maximum value), which is stored in
answer. - The output would be: 9
If you do not assign the returned value to a variable, it will not be saved and cannot be used later.
Worked example - Creating and calling a function
Write a function that takes two numbers as parameters and returns their sum. Show it working with the numbers 12 and 8.
Step 1: Define the function
Step 2: Call the function and store the result
result = addNumbers(12, 8)
Step 3: Use the returned value
PRINT(result)
Step 4: Interpretation
The function returns 20, which is stored in result and printed. This shows how functions can compute and return values for further use.
Benefits of using subroutines in programs
Subroutines make programs more efficient and easier to maintain. They help avoid repeating the same code multiple times, which saves time.
Key advantages
- Code reuse - Write the instructions once and call the subroutine whenever needed.
- Improved readability - Breaking code into named subroutines makes the program easier to understand, as each part has a clear purpose.
- Built-in functions - Many programming languages come with ready-made functions, such as ones for mathematical operations or string handling, which you can use without writing them yourself.