teach-ict.com logo

THE education site for computer science and ICT

3. Building a subprogram

The previous page has this rather messy blocks of code;

 
                 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 

Really though, this program is just the same set of commands (in red) carried out over and over with different inputs.

The first step in creating a subprogram to handle these commands is to define the subprogram. This involves:

  • Giving the subprogram a name
  • Defining the inputs (parameters) it needs.
  • Defining the commands to be carried out using those inputs.
  • Defining which outputs, if any, to return to the main program.
    • A Subprogram that return a single output to a program is called a function.
    • Subprograms that do not return an output or return more than one item are called procedures

In pseudocode, defining a procedure for the above commands would look like this:


    PROCEDURE Numbering(FirstNumber)
	
    BEGIN PROCEDURE
  
         IF FirstNumber > 100 THEN
         SET FirstNumber TO 100
         SET SecondNumber TO FirstNumber * 0.15
         SEND SecondNumber TO DISPLAY
				
    END PROCEDURE 

The name of the created procedure is 'Numbering', it needs 'FirstNumber' as a parameter, it carries out the commands in red, and it does not return a value to the main program so it is a procedure rather than a function.

 

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?