Programming Techniques: Repetition & Subroutines

In this section we tackle two key aspects of programming and control: repetition and subroutines. (OK we did subroutines earlier but now we get to do them again.)

The third major construct in programming is repetition or looping, doing something over and over again. One way to create a loop is to specify the number of times you want the same thing to be repeated. Another way is to repeat the same action until a condition is met such as switching on an input.

To create a loop with a fixed number of repetitions you will need to set up a counter:

The letter 'a' is a variable, that is it can take on values such as 1, 2, 3, etc. We want 'a' to take on values 1, 2, 3, 4 and 5 so that we can count the number of times an event occurs. The next step, therefore, is to add 1 to 'a':

Finally we ask a question: is 'a' equal to 5:

If 'a' has reached 5 then we stop because we have performed our task the required number of times. If 'a' has not reached 5 the flow of control loops back round to wherever we require in order to continue counting. For example:

Ferris Wheel

We apply these counting and looping techniques to programming the Ferris wheel. You should be able to define three distinct stages of the ride. As before you should experiment with the inputs and outputs toolbar in Flowol so you know which controls you will need. Note that the Ferris wheel has inputs (buttons and a gate) and outputs (lights) as well as a motor. You should be able to develop the program/flowchart yourself from this point.

Note that the London Eye works in a rather different way to a traditional fairground Ferris wheel. The London Eye revolves continuously at a slow speed and passengers must board by stepping into a moving pod. Fairground Ferris wheels do things differently...

Subroutines

When you have worked out the three stages of a ride on a Ferris wheel you will be in a position to define subroutines for some of them. A subroutine is an action that you can call from another part of a program so you don't need to include the code for what it does in the main part of the program. This reduces the length of the main part of the program and it allows you to call the code in the subroutine from wherever you want. Once you have designed a subroutine you have, as they say, 'invented the wheel' and you don't need to re-invent it.

To define a subroutine use the rounded rectangle tool and select 'Sub' as the type.

Put whatever actions you require into the subroutine and finish it with a Stop symbol. Now you can call the subroutine by name in a process box.

Note that you can call the subroutine as many times as you like in one box.

Modularity

The Ferris wheel is a good example of a problem that can be broken down into two or more separate parts or modules. Problems like this are said to have modularity, they can be broken down so they are easier to understand and process into flowchart diagrams or program code. Breaking problems down like this is one of the key contributions of computer science to thinking and problem solving in general.