Until now, the example programs presented contained only a main program. The main program actually is a subprogram. The only thing that distinguishes the main program from other subprograms is the fact that it is the first subprogram to run when a new process is created. For this reason, the main program is also called the entry point.
Within each subprogram there are variable definitions and statements, exactly like we have seen in our main programs.
Fortran offers two kinds of subprograms, which are implemented almost exactly the same way. The only difference between them is in how they are invoked, or called.
When a subprogram is called, the program "jumps" to the subprogram, runs it, and then returns to where the subprogram was called and continues executing the caller.
A function is a subprogram that is invoked by using it in an expression. Since a function call is part of an expression, it must return a value, which is used in the expression.
The main program calls other subprograms, which may in turn call other subprograms, and so on.
The call tree shows which subprograms call other subprograms. The call tree should have the same structure as the top-down design!
Tree diagram for sort
You have already seen
examples of function calls such as sin()
,
abs()
, mod()
, etc.
Consider the following statement:
y = x ** 2 + sin(x) - 5.6
When this statement is executed, the program performs the following steps:
A Fortran subroutine is a subprogram that does not return a value, and is not called as part of an expression. A Fortran subroutine call is a separate statement by itself. You have seen examples of subroutine calls with write, print, and read. Consider the following program segment:
print *, 'Enter the radius of the circle:' read *, radius print *, 'The area of the circle is', PI * radius * radius
The sequence of events is as follows:
radius
.
radius
,
and stores the value in the memory location
that radius
represents.
PI * radius * radius
to the print
subroutine.