Structured Programming  «Prev  Next»
Lesson 8Subprograms
ObjectiveDefine a subprogram.

What is Subprogram?

Earlier in this module it was stated that the primary philosophy of structured programming is to partition a large program into a number of smaller programs. Let's now look at a simple example of how this is done. Consider the GoForARide program from the previous lesson.
GoForARide
Put on cycling clothes
For 2 hours
  Ride for 15 minutes
  Drink from water bottle
Remove cycling clothes
Take a shower

The statement:
Drink from water bottle

seems pretty simple, but for the sake of discussion let's suppose that it involves the following.
If the water bottle is empty
  Find a source of water
  Open the water bottle 
  Fill it with water
  Close the water bottle
Drink some water

Thus our GoForARide program really should be:
GoForARide2
Put on cycling clothes
For 2 hours
  Ride for 15 minutes
  If the water bottle is empty
    Find a source of water
    Open the water bottle 
    Fill it with water
    Close the water bottle
  Drink some water
Remove cycling clothes
Take a shower


This is starting to get a little complicated, so in order to simplify the program we might create a subprogram called DrinkFromWaterBottle that looks like:
DrinkFromWaterBottle
If the water bottle is empty
  Find a source of water
  Open the water bottle 
  Fill it with water
  Close the water bottle
Drink some water

And then call this subprogram from our GoForARide program as in the following (roll your cursor over the highlighted line of code to see a graphic representation of this subprogram being executed):
Put on cycling clothes
For 2 hours
  Ride for 15 minutes

Subprogram Execution

Subprogram Execution
Subprogram Execution

As the name subprogram suggests, a subroutine behaves in much the same way as a computer program that is used as one component in a larger program or another subprogram. A subroutine is often coded so that it can be called several times from several places during execution of the main program, including from other subroutines, and then return to the next instruction after the initial call once the subroutine's task is completed.

How Subroutines are used in Fortran

In Fortran, a programming language widely used in scientific and engineering applications, a subroutine is a fundamental construct that enables the modularization and reuse of code. A subroutine in Fortran is designed to perform a specific task or function within a larger program, encapsulated in a block of code that is separate from the main program body. The primary purpose of a subroutine is to encapsulate sequences of statements and computational operations that need to be executed multiple times within a program, or across different programs, without the need for code duplication. A subroutine is defined by a unique name and can optionally have a list of parameters, known as arguments, which allow data to be passed to and from the subroutine. When a subroutine is called or invoked from the main program or another subroutine, control of the program is temporarily transferred to the subroutine. The subroutine performs its designated operations, which can involve processing input data, performing calculations, manipulating arrays, or executing any other tasks as defined by its internal code. Upon completion of its tasks, control is returned to the calling program at the point immediately following the call, allowing the program to continue its execution. Subroutines can return results to the calling program through arguments that are passed by reference, meaning that any changes made to the arguments within the subroutine are reflected in the corresponding variables in the calling program.
The use of subroutines enhances program clarity, maintainability, and scalability by allowing complex problems to be broken down into smaller, more manageable units of code. This modular approach facilitates debugging, testing, and modification of the program, as each subroutine can be developed, tested, and maintained independently of others. In summary, the function of a subroutine in Fortran is to provide a mechanism for code modularization, enabling the encapsulation of specific tasks or functionalities within self-contained units of code. This promotes code reuse, reduces redundancy, and improves the overall structure and manageability of Fortran programs.

Making the task of drinking from the water bottle a subprogram has noticeably simplified the GoForARide program, but there is an additional benefit in that the DrinkFromWaterBottle subprogram can be used in other programs as well. Imagine how this subprogram might be used in programs such as DoYardWork or GoForAHike. The next lesson concludes our look at the fundamental concepts of structured programming.

Structured Programming Terms

Click the Exercise link below to check your understanding of structured programming.
Structured Programming Terms

[1]Subprogram: A program within a program. Also called a function, method, procedure, or subroutine.

SEMrush Software