Subroutines and functions
2. Spotting repeating code
Look at the block of code below
 
                 FirstName = "Joe"
                 SecondName = "Smith"
                 FullName = FirstName + SecondName
                 Print 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
                 Print FullName
 
                 FirstName = "Louise"
                 SecondName = "Brown"
                 FullName = FirstName + SecondName
                 Print FullName                
                 
                 FirstName = "Mandy"
                 SecondName = "Jones"
                 FullName = FirstName + SecondName
                 Print FullName
                 
     This will work perfectly well. 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 subprocedure or subroutine as explained on the next page.
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 subroutine?
