±«Óătv

Using subprograms to produce structured code

are small programs that are written within a larger, main program. The purpose of a subprogram is to perform a specific task. This task may need to be done more than once at various points in the main program.

There are two types of subprogram:

  • procedures
  • functions

Benefits of using subprograms

  • Subprograms are usually small in size, which means they are easier to write, test and debug than programs. They are also easy for someone else to understand.
  • Subprograms can be saved separately as modules and used again in other programs. This saves time because the programmer can use code that has already been written, tested and debugged.
  • A subprogram may be used repeatedly at various points in the main program. However, the code only has to be written once, resulting in shorter programs.

Procedures

A procedure is a subprogram that performs a specific task. When the task is complete, the subprogram ends and the main program continues from where it left off. For example, a procedure may be written to reset all the values of an array to zero, or add some values together.

A procedure is created using the following pseudo-code syntax:

PROCEDURE identifier ( parameter(s) )
                    BEGIN PROCEDURE
                         procedure code
                    END PROCEDURE

A parameter allows a value to be passed in to the procedure. The name of each parameter can be used inside the procedure to access the value the procedure was called with. This is illustrated in the next example.

The following procedure - called add2numbers - takes two parameters called number1 and number2, adds the values assigned to them together and displays the answer:

PROCEDURE add2numbers(number1, number2)
                    BEGIN PROCEDURE
                         SET total TO number1 + number2
                         SEND total TO DISPLAY
                    END PROCEDURE

A procedure is run by calling it. To call it, programmers use the procedure name and include any parameter values that the procedure needs, eg:

add2numbers(2,5)

This prints the number 7 on the screen.

Functions

A function works in the same way as a procedure, except that it processes data and returns a result back to the main program.

For example, a function might be written to turn Fahrenheit into Celsius, eg:

FUNCTION f_to_c(temperature_in_f)
                    BEGIN FUNCTION
                         SET temperature_in_c TO
                         (temperature_in_f – 32) * 5/9
                         RETURN temperature_in_c
                    END FUNCTION

A function is run by calling it from the main program. To call it, programmers use the function's identifier, the parameter value to be passed into the function, and a variable for the function to return and assign a value into, eg:

celsius = f_to_c(32)

This would result in the value of celsius being 0.

Built-in functions

Many programming languages include built-in, ready-made functions, such as:

  • int - converts strings or floats into integers
  • str - converts a number into a string
  • asc - finds the ASCII number of a character

Additionally, some languages allow functions to be added in from external files called libraries. Libraries contain pre-written, tested functions that extend the functionality of a language.