Explore functions
Now that you have started with procedures we can move on to explore functions. These are just like procedures, but return a value and can therefor be used in expressions. As with procedures, let’s build some small utilities that may be useful elsewhere, and create a test programs to explore how they work.
Read String
In our previous programs we have often needed to read text from the user in our terminal programs. The code would have looked something like this:
While it is only a couple of lines, we will want to make use of this in many places as we interact with the user. Creating a function to do this for us will save us effort in the long run, and will give us a nice little utility that we can make use of. To build this we need to think about the function in general.
What do we want it to do?
What information can we pass it so that it is usable in as many places as possible.
We could create a function to read in the user’s name, but we will also want to be able to read in other values. So we could add a parameter to the function, so that the caller can pass in the message they want shown. We can then return the value read by read_line
so that the caller can store it where ever they want. This gives us the following as our specification for this function:
Function | Read String |
Parameters | prompt : The message to show to the user |
Returns | The string entered at the Terminal |
Description | Displays a prompt, reads the string entered at the terminal, and returns this string to the caller. This can be used to read values from the user. |
Write the Code and Test
Let’s write the code for this now. Here is a good start at the test program:
Read Integer
Looking at other terminal input in our programs we also have places where we want to read integer values from the user. For example, the following code was in our change calculator program. We can simplify this a little by using our read_string function, but we could further refine this by creating a read_integer
function that performs this validation as well.
The following table captures our specification for the read integer function. As with read string, we can accept a prompt parameter to allow this to be used in different situations.
Function | Read Integer |
Parameters | prompt : The message to show to the user |
Returns | The integer entered at the Terminal |
Description | Displays a prompt, reads the string entered, safely converts this to an integer, and returns the value. This will display error messages when there is invalid input, and will ask the user to enter again. |
We can test this out by extending our program to read in an integer. Have a go at coding the read integer function and test this out.
Read Integer - with a range
Thinking about this further, we could also create a function that allows us to read an integer within a given range. For example, reading an integer between 1 and 10. This will be particularly useful when we create something like a menu where they need to choose one of the menu options.
Have a go at thinking about the specification for this, and then code this up and extend your test program to check that it works.
-
I used the following to test out this function:
Here is the specification that I used for this function. I decided to overload read integer - providing two versions, one that can be called with just the prompt and another with the prompt and a range.
Function Read Integer Parameters prompt
: The message to show to the userlow
: An integer representing the smallest value to returnhigh
: An integer representing the largest value to returnReturns The integer entered at the Terminal - this will be between low and high (inclusive) Description Displays a prompt, reads the string entered, safely converts this to an integer and ensure this is within the range indicated, and returns the value. This will display error messages when there is invalid input, and will ask the user to enter again. The code for read integer with a range is shown below.
Try testing this out by entering text instead of numbers, as well as entering values outside the requested range. Think about where these things are being checked, and what code is validating this data entry.
Test it out
See if you can create an equivalent set of function to read in double values. Remember to test out each of these as you add them. This will provide you with a useful set of functions to handle terminal a range of input scenarios.