Subroutines let you name a block of code once and reuse it. In this lesson we separate a procedure from a function, see why we bother writing subroutines at all, and learn to pass values in with parameters and get a value back with a return.
Slide 1 / 4
Two kinds of subroutine
A subroutine is a named block of code you can call from elsewhere in a program, so you write the instructions once and reuse them many times.
A procedure carries out a task but does not send a value back to the line that called it; in Cambridge pseudocode it is written with PROCEDURE ... ENDPROCEDURE.
A function carries out a task and RETURNs a single value to the point where it was called, so the returned value can be used in an expression or stored in a variable.
The key difference to remember is that a function returns a value and a procedure does not.
Check you've got it
0/4
1. What is the essential difference between a function and a procedure?
2. Which of these is NOT a reason for using subroutines?
3. In the call Total <- Add(4, 6), what is the role of the values 4 and 6?
4. A variable is declared inside a function and used only within it. What is this variable called?