Structured Programming
Subroutines
Structured programming refers to the use of subroutines__ to split up code into smaller chunks. It also makes use of __selection (IF statements) and iteration (FOR/WHILE loops) to affect the way the program runs.
A subroutine is a named ‘out of line’ block of code that may be executed (called) by simply writing its name in a program statement.
Functions and procedures are both examples of sub-routines. They are sections of code that can be reused multiple times. Both can have arguments (also known as parameters) passed into them. A function will return a value but a procedure will just ‘do something’ without returning a value.
Returning a value means that something is produced as a result of running that subroutine, and that something can be used outside of the subroutine.
Function Interface
In general, a function interface includes:
- Identifier – the name of the function (this should be descriptive)
- Parameters – these are the function input (these must be passed in to the subroutine when it is called)
- Process – the ‘doing’ stuff
- Output – the return value
This is how a function is written in pseudocode:
__SUBROUTINE __Identifier(parameters)
RETURN value
ENDSUBROUTINE
This function will only run if it is called using the following code:
Identifier(parameters)
If the subroutine has a return value, it is also important that we use a variable to capture this when it is called.
value ← Identifier(parameters)
Here is an example of a piece of pseudocode using a subroutine. What will it output?
SUBROUTINE add(a, b)
value ← a + b
RETURN value
ENDSUBROUTINE
new_value ← add(3, 5)
OUTPUT new_value
- Output:
- 8
Advantages of the Structured Approach
The structured approach was developed to aid debugging. It aims to give programs a logical structure which makes them easier to understand and modify. Modules (sections of code) can also be reused which reduces development time and it is easier to independently test__ __the individual modules.
It is important to name subroutines, variables, constants and parameters using descriptive names, as this allows other programmers to easily follow the code that you have written. You should also make liberal use of comments to explain sections of code. Comments are lines of code denoted by # in pseudocode that the computer ignores. They are only there solely for the programmers to read.
Local and Global Variables
Variables created within a subroutine are called Local Variables. These are only available for use in the function or procedure and once the subroutine ends, they are destroyed.
Variables created outside of a procedure or function are known as Global Variables. It is best to avoid using these as they take up memory and increase the chance of naming clashes which could lead to bugs.
If we want to get a value INTO a subroutine, we need to pass it in as a parameter. This is a value inside a bracket after the subroutine identifier. When we call the subroutine, we need to specify what these values are.
If we want to get a local variable OUT OF a subroutine, we need to tell the function to RETURN that value. This is then stored as a variable in the program that called the function in the same way you would store a value from a user’s input.
In this code, identify the local and global variables. In each, write local or global.
SUBROUTINE add(a, b)
value ← a + b
RETURN value
ENDSUBROUTINE
new_value ← add(3, 5)
OUTPUT new_value
- a
- local
- b
- local
- new_value
- global
- value
- local
- In the code above, how many parameters does the function add have?
- 2
- In the code above, how many return values does the function have?
- 1