A subroutine is a named block of code that does one job, so you can use it many times without rewriting it. Parameters are the named inputs it needs, and a function also returns an answer.
This lesson belongs to the programming structure module. It follows local and global scope, because parameters are the clean alternative to reaching into global variables.
How do I design a subroutine?
Work through four decisions in order.
- Name the job. Use a name that says what it does, such as
HireFee. - List the inputs. What must the subroutine know? Each one becomes a parameter, with a type.
- Decide the output. If the caller needs an answer, write a FUNCTION with
RETURNSand a type. If the job is only to do something, write a PROCEDURE. - Keep the body self-contained. Use only parameters and local variables inside.
Worked example
A fictional community hall charges an hourly rate. Bookings of 5 hours or more get 10% off the total. Write a function that returns the fee.
Inputs: the number of hours (INTEGER) and the rate per hour (REAL). Output: the fee (REAL).
FUNCTION HireFee(Hours : INTEGER, Rate : REAL) RETURNS REAL
DECLARE Fee : REAL
Fee ← Hours * Rate
IF Hours >= 5 THEN
Fee ← Fee * 0.9
ENDIF
RETURN Fee
ENDFUNCTION
OUTPUT HireFee(6, 20.00)
OUTPUT HireFee(3, 20.00)
Trace the first call, HireFee(6, 20.00):
| Line | Hours | Rate | Fee |
|---|---|---|---|
| Call | 6 | 20.00 | not set |
| Fee ← Hours * Rate | 6 | 20.00 | 120.00 |
| IF Hours >= 5 is TRUE | 6 | 20.00 | 120.00 × 0.9 = 108.00 |
| RETURN | 6 | 20.00 | 108.00 returned |
The second call has Hours = 3, so the condition is FALSE and the function returns 3 × 20.00 = 60.00. The two outputs are 108.00 and 60.00.
The same function in Python, for comparison:
def hire_fee(hours, rate):
fee = hours * rate
if hours >= 5:
fee = fee * 0.9
return fee
Which mistake catches students most often?
Mistaken answer: calling
HireFee(25, 4)when the booking is 4 hours at RM25.The first argument goes to
Hoursand the second toRate, by position. HereHoursbecomes 25, so the discount applies and the function returns 25 × 4 × 0.9 = 90, not the correct 4 × 25 = 100.
Nothing crashes, which is why this slip is hard to spot. The correct call is HireFee(4, 25.00), which gives 100.00. When you write a call, check each argument against its parameter in order.
A second mistake is putting INPUT or OUTPUT inside a function that is meant to calculate. That is covered in separating logic from input and output.
Self-check
1. In HireFee(Hours : INTEGER, Rate : REAL), which are the parameters, and what are the arguments in HireFee(8, 15.00)?
Show answer
Parameters: Hours and Rate. Arguments: 8 and 15.00.
2. State the value returned by HireFee(8, 15.00).
Show answer
8 × 15.00 = 120.00. Hours is 8, which is at least 5, so the discount applies: 120.00 × 0.9 = 108.00.
3. Write a procedure Greet that takes a STRING parameter Name and outputs “Hello, ” followed by the name. Show the call for the fictional name Aina.
Show answer
PROCEDURE Greet(Name : STRING)
OUTPUT "Hello, " & Name
ENDPROCEDURE
CALL Greet("Aina")
The output is Hello, Aina. It is a procedure because nothing needs to be returned.
Where does this lead next?
A function hands its answer back with RETURN. To follow exactly which value comes back and when, move on to tracing a returned value. You can run HireFee in the safe Python reasoning sandbox and step through the trace in the restricted pseudocode trace trainer.
If designing the parameter list is where you freeze, our online one-to-one Computer Science tuition works from the tasks you have been set.