In these tasks you have to use the skills learned in the previous exercises to create a new program based on the requirements below.
- Define four subroutines – add, subtract, multiply, divide that add multiply etc two numbers and return the result. Each should have two integer number arguments.
- The user is asked to input two numbers. These numbers will be passed as arguments into one of the subroutines.
- The user is asked to input 1 to add, 2 to subtract etc.
- If they input 1, call the ‘add’ subroutine, input 2 calls the ‘subtract’ subroutine etc
- Output the returned result as part of a sentence.
Subroutine: A subroutine is a set of actions that is given a single name (identifier) in a very similar way to a variable or list. It is just a ‘package’ for code. You can write any code inside a subroutine, including calling other subroutines
Functions: A subroutine can also return a result (data) to the main program as its final action. This type of subroutine is called a function
Help! My code doesn’t work
Make sure that you check for the following things:
- The subroutine name is identical everywhere it is used (capitals matter)
- The subroutine call is not indented inside the subroutine
- The subroutine is defined before it has been called