Instead of writing a huge, monolithic piece of code, you can break a program down into smaller pieces, called subroutines. These allow for increased modularity in an application. It also makes it much easier to work on a project with multiple people involved because they can each write subroutines to work together and produce a complex system quickly.
It is much easier to solve a problem by breaking it down into subroutines.
All subroutines are reusable, meaning that if you write a utility to authenticate a user, you can call the same subroutine from multiple places, keeping code tidy and reducing time-wasting.
Here is an example subroutine written in Python:
When this code is executed, it will return the literal
3. This is because we pass the values
2 to the subroutine, which then adds them together. The answer is printed out because we save the result in a variable and then display it with the
Here is a demonstration of the program being executed:
Taking this a step further, we could ask the user to pick the values to add together. We would do this using the following code:
And the result looks like this when we run it:
Types of subroutine
In terms of Python
The following uses Python keywords as examples, but the same logic can be applied to any language.
Because this code uses the
return keyword, it is a function.
The other type of subroutine is a procedure, this would not use the
A procedure does not return any value to the logic that called it, whereas a function does. This is the only difference between the two types.
A function would typically be used where a response is needed or the status of the action needs verifying. If you do not need an answer, then a procedure may be the best choice.
Subroutines are made out of the basic building blocks found here.
Task: Write a subroutine
A procedure that:
- Receive a person’s name
- Adds “from PHHS” to the name
- Prints the product
Next, a function to convert celsius to fahrenheit:
Just like with variables, the name of a subroutine is officially called an identifier. Also - just like with variables - the identifier should be descriptive as it improves code readability.
To extend the functionality of a subroutine (or any of your code), modules are very useful. Find out more here