Choosing Building Blocks
When designing the building blocks that will make up your code, you need to think about problem, and try to imagine how you can construct the digital reality (your program) so that it produces the desired outcomes. This will involve creating variables to capture data, and functions and procedures to carry out actions on this data.
Identifying Functions and Procedures
In reviewing the Guess that Number game, there appear to be two main processes that need to be performed: play game and perform guess. We can put these into our digital reality by creating functions or procedures in the program’s code.
Play Game
code can be implemented as a procedure. It will be responsible for running the process of the game, starting with telling the user that the program has ‘thought of a number’, through to coordinating the guesses, ending only when the user gets the answer of runs out of guesses.
Perform Guess
can be created to contain the logic for performing a single guess. This can be a function, so it can return a boolean value indicating if the user has guessed the target number. The code in Perform Guess
will be responsible for asking the user to enter a guess, and then giving them the feedback on their guess. As this has the details of the guess. The result from this will be needed when the code returns to Play Game
, so that it can determine if the game has ended.
The Perform Guess
code will also need to accept parameters to tell it what the current target
value is. This data will exist within the Play Game
code, so Perform Guess
will need a parameter to accept the target
value.
A nicety may be to tell the user which guess they are up to. We can store this in Play Game
, so a second parameter can be added to allow Play Game
to pass in the guess number
along with the target
number.
In addition to these we also added a Print Line
procedure. This will display a line of ’-’ characters at the end of the game before the user is asked if they want to play again. A length
parameter will enable the caller to indicate how many of these characters are printed on the line.
Visualising the Code
The structure chart showing these functions and procedures for this game is shown in below. These help show what we are creating visually. At this level you can see that this is a game that involves performing guesses. A good design for a program will connect with us, helping us see what it is all about. These visualisations help us picture the design, and think about how it works.
Structure Chart
A structure chart shows the structure of the solution, visually showing the functions and procedures and the calls between them. Each box in the diagram represents either a function or a procedure. The arrows show one function/procedure calling another. Along the arrow, you have indicators of data flows showing values passed to parameters and returned from functions. These data flows are shown alongside the call arrow, with their own smaller arrow to indicate the direction of the flow.
By reading the structure chart above, you can see which parts of the code are functions and which are procedures. Perform Guess
is a function as it have to return data to Play Game
. Both PlayGame
and PrintLine
are procedures as they do not return anything to their callers.
We can also see the parameters we are including in the design. Perform Guess
will need two parameters for the number of the guess and the target, and Print Line
has a length parameter. As you think through the structure of your program, you can use this visualisation to make sure that each function and procedure has the data it needs, and can pass the required information to the functions and procedures it calls.
For example, we can look at Play Game
, it accepts no parameters but will need to be able to pass Perform Guess
which guess number we are up to, and the target. Both of these will need to be local variables in Play Game
, which will work for this program. In other cases, you can use this to identify additional parameters that the function or procedure may need to make these calls.
Control Flow
Having chosen the functions and procedure we want to build, the next step is to design the control flow that will enable these functions and procedures, and the program itself, to achieve their goals. You can think of each function/procedure as having a certain responsibility that it must meet in order for the overall solution to work.