What subroutines are
- A subroutine is a named block of code that carries out a specific task. It can be called (run) whenever it is needed.
- A procedure carries out a task. A function carries out a task and returns a value. (AQA calls both 'subroutines'; Edexcel calls them 'subprograms'.)
Parameters and return values
- Parameters are the variables in the subroutine's definition that receive data. The actual values passed in when it is called are called arguments.
- A function area(length, width) that returns length × width gives 12 when called as area(3, 4).
Local and global variables
- A local variable only exists inside its subroutine. A global variable can be used anywhere in the program.
- Local variables are good practice: the subroutine is self-contained, and its variable names can't clash with the rest of the program.
Why use subroutines?
- Code is written once and reused, so programs are shorter. Each subroutine can be tested separately, which makes debugging easier.
- Programs are easier to read and maintain, and different programmers can work on different subroutines at the same time.
- A structured approach uses subroutines with clear parameters and return values, and good documentation.
Key terms
- Subroutine
- A named block of code that carries out a specific task.
- Procedure
- A subroutine that carries out a task.
- Function
- A subroutine that returns a value.
- Parameter
- A variable in a subroutine's definition that receives a value.
- Argument
- The actual value passed into a subroutine when it is called.
- Return value
- The value a function sends back to where it was called.
- Local variable
- A variable that only exists inside its subroutine.
- Global variable
- A variable that can be used anywhere in the program.
Quick questions on Subroutines and parameters
- What is the difference between a function and a procedure?
- A function returns a value; a procedure doesn't have to: Both carry out a task.
- What is a parameter?
- A variable that receives a value passed into a subroutine: For example, length and width in area(length, width).
- A function area(length, width) returns length × width. What does area(3, 4) return?
- 12: 3 × 4 = 12.
- What is a local variable?
- A variable that only exists inside its subroutine: It disappears when the subroutine ends.
- Why is using local variables good practice?
- The subroutine is self-contained and names can't clash: Other parts of the program can't change them by accident.
- Which of these is a benefit of using subroutines?
- Code can be reused instead of repeated: Write it once, call it many times.
- What is the value passed into a subroutine when it is called?
- An argument: The parameter receives the argument.
- Which keyword sends a value back from a function?
- RETURN: The value goes back to where the function was called.
Subroutines and parameters: questions and answers
- What is a global variable?
- A variable that can be used anywhere in the program. It is declared outside all the subroutines.
- What does splitting a program into subroutines make easier?
- Testing each part separately. Each subroutine can be checked on its own.
- A function double(n) returns n × 2. What does double(double(3)) return?
- double(3) = 6, and double(6) = 12.