Subroutines

<< Previous: Nesting Next: Arithmetic operations >>

About subroutines

Subroutines are self contained sections of code, stored under an identifier that is used to call them when you want them to be run. They may or may not take one or more parameters, which are values passed to the subroutine from the main program. More about this later in the local and global variables section. There are two types of subroutines:
- Procedures
- Functions
The way to differentiate between procedures and functions is simply that procedures execute their code and return control to the main program while functions execute their code and then return one or more values to the main program. This means that while a procedure can simply be called on its own line of code but a function needs to be called in a storage location or several if more than one value is returned.

You can see in the subroutine itself whether there is a return value or not and you can also identify procedures and functions by the way they are called. If they are called on their own line of code they are procedures. If they are called to return values into one or more storage locations then they are functions. The second function shows how returning more than one value is handled. It also starts to show how subroutines ca be combined to make more complex code. You should be able to tell that the version of add2numbers() that has been called is the procedure as it was called on its own line.

Local and global variables

Global variables are declared in the main program and are available in all subroutines within the program. Local variables are defined within a subroutine and only exist while that subroutine is being executed. If a subroutine declares a local variable with the same name as global variable it is the local version that will be used. This is generally to be avoided anyway as it makes programs more confusing to understand.

In the add2nums function above answer is a local variable. In the function get2nums both num1 and num2 are local variables. If you execute these subroutines and then try to output these values then you will get a syntax error because there will be no variable with that name. In effect when you pass values into a subroutine you are making local copies of those variables to use in your subroutine.

It may seem like pointless extra work to use local variables but it is actually common practise to do so. There are many advantages to using local variables. Subroutines are self contained and so there is no risk of conflicts with global names in the main program because all names are local. In the add2nums procedure above, once passed the choice1 and choice2 variables become the local variables num1 and num2. Being self contained also means they can be tested independently making them easier to maintain and debug. A programmer who wants to use the subroutine does not need to know how it works just what values to pass to it and what it will return.

Advantages of using subroutines

Using subroutines is an example of using decomposition to solve problems. It means they can be tested individually and so edited and then changed in the main program after they have been tested. They can be used several times and if they are particularly useful can be added to a subroutine library that can be called into other programs. This also allows multiple programmers to work on the same program at the same time which is how most major pieces of software are made. If a program needs to be changed, then it is easier to identify and modify the appropriate subroutine than to search through a long program code listing.

Knowledge check


Questions:
Correct:

Question text


<< Previous: Nesting Next: Arithmetic operations >>

© All materials created by and copyright S.Goff