Section 1 Chapter 5 - Subroutines Flashcards
Subroutine
A block of code which performs a specific task within a program
Function
A subroutine which returns a single or multiple values
Procedure
A subroutine that does not return a value
Global Variable
Variable that can be used anywhere in the program
Local Variable
Variable that can only be in a subprogram and only exist during the execution of the subprogram
Modular Programming
A ‘top down’ method of programming in which the problem is broken down into a number of subtasks
Advantages of using subroutines (2)
- Splits the program into simpler tasks
- Reduces duplicate code
The use of parameters
Allows different parts of the program to pass data without the use of global variables
Subroutine interface
The parts of a subroutine that are in direct contact with the calling program. This consists of the subroutine name, parameters and returned value(s)
Why local variables are a good practice
If a value is only needed within a subroutine then it is good practice to make it a local variable. This reduces the total number of variables outside the subroutine and makes the program clearer.
Difference between global and local variables
Global variables can be accessed and changed anywhere in the program whereas local variables can only be accessed or changed inside their subroutine