teach-ict.com logo

THE education site for computer science and ICT

2. Spotting repeating code

Look at the block of code below

 
                 FirstName ??? "Joe"
                 SecondName ??? "Smith"
                 FullName ??? FirstName + SecondName
                 OUTPUT(FullName)

The first two lines declare the first and second name of a person held as variables. The third line then concatenates the names and the fourth line prints out the full name. All well and good. It is a very efficient piece of coding.

Now let's consider that three people need to have their names printed out. The pseudocode below shows how this may be written

 
                 FirstName ??? "Joe"
                 SecondName ??? "Smith"
                 FullName ??? FirstName + SecondName
                 OUTPUT(FullName)

 
                 FirstName ??? "Louise"
                 SecondName ??? "Brown"
                 FullName ??? FirstName + SecondName
                 OUTPUT(FullName)                
                 
                 FirstName ??? "Mandy"
                 SecondName ??? "Jones"
                 FullName ??? FirstName + SecondName
                 OUTPUT(FullName)
                 

This will work, but do you notice how often the same two lines code are repeated i.e. the FullName and Print statements?

This is inefficient and it also makes it more likely that a programmer will introduce a mistake into the code.

Carrying out the same task in different parts of the program is so common, that a special way of avoiding it has been developed. It is called a procedure.

 

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?