2. Spotting repeating code
Let's start with a fairly simple program:
SET FirstNumber TO 37
IF FirstNumber > 100 THEN
SET FirstNumber TO 100
SET SecondNumber TO FirstNumber * 0.15
SEND SecondNumber TO DISPLAY
END IF
Now imagine a program that wants to carry out the same steps, but for a bunch of different numbers. It might look like this:
SET FirstNumber TO 137
IF FirstNumber > 100 THEN
SET FirstNumber TO 100
SET SecondNumber TO FirstNumber * 0.15
SEND SecondNumber TO DISPLAY
END IF
SET FirstNumber TO 540
IF FirstNumber > 100 THEN
SET FirstNumber TO 100
SET SecondNumber TO FirstNumber * 0.15
SEND SecondNumber TO DISPLAY
END IF
SET FirstNumber TO 320
IF FirstNumber > 100 THEN
SET FirstNumber TO 100
SET SecondNumber TO FirstNumber * 0.15
SEND SecondNumber TO DISPLAY
END IF
This will work, but it looks a little clunky. The same lines of code are repeated over and over again. This is inefficient and it also makes it more likely that a programmer will introduce a mistake into the code.
It would be much more efficient to give that batch of steps a label, and then just use that label to refer to it whenever necessary.
This is what subprograms are for.
Challenge see if you can find out one extra fact on this topic that we haven't already told you
Click on this link: What is a procedure?