0% found this document useful (0 votes)
57 views

Fortran Subprograms

The document discusses breaking large Fortran programs into smaller subprograms called functions and subroutines. Functions and subroutines allow programs to be structured, reusable, and isolate code. They are defined internally or externally and communicate through arguments passed between the main program and subprograms.

Uploaded by

Prasad Majee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Fortran Subprograms

The document discusses breaking large Fortran programs into smaller subprograms called functions and subroutines. Functions and subroutines allow programs to be structured, reusable, and isolate code. They are defined internally or externally and communicate through arguments passed between the main program and subprograms.

Uploaded by

Prasad Majee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Fortran Subprograms

Until now, all of the programs have essentially been single, fairly small programs.
However, as we scale up into larger programs, this methodology will become
more difficult. When developing larger programs, it becomes necessary to break
larger programs up into multiple, smaller more manageable pieces. Then, during
program development, it is possible to focus on each subsection or piece
individually and then combine the results into a final complete program. And, for
very large projects, multiple people may work on different parts of the program
simultaneously. Some of the key advantages of developing a program using
functions and/or subroutines include:
● Structured development of programs
● Reuse of subprograms
● Isolation of subprograms

Fortran subprograms are the mechanism to break a large program into multiple
smaller parts. This allows for a more comprehensive program design.

Subprogram Types
There are two types of Fortran subprograms: functions, and subroutines, each of
which is explained in the following sections.

Program Layout
The functions and subroutines can be defined as either internal or external.
Internal functions and
subroutines are defined within the program statement (i.e., before the “end
program <name>”
statement). The basic layout for both internal and external subprograms are as
follows:
program <name>
<declarations>
<program statements>
contains
<internal functions or subroutines>
end program <name>
<external functions or subroutines>
Where a combination of both or either internal or external routines is allowed.

Arguments
When writing and using Fortran subprograms, it is typically necessary to provide
data to a subprogram and/or to obtain results back from the functions or
subroutines. This information, in the form of variables, is referred to as an
argument or arguments. The argument or arguments in the calling routine are
referred to as actual arguments, and the argument or arguments in the function or
subroutine are referred to as formal arguments. The formal arguments take on the
values that are passed from the calling routine. The only way to transfer values in
to or out of a subroutine is through the arguments. A function typically passes
values in through the arguments with a single return value (via the function
name). All other variables are independent and isolated.

Using Functions and Subroutines


Before a function or subroutine can be used, it must be defined or written. Once
defined, the function or subroutine can be used or called. A function or subroutine
is called by using its name as we have done with the intrinsic functions. When a
program uses a subroutine, it is called with a call statement. When a program uses
a function, it is used by name and returns a result which must be assigned
somewhere appropriate (e.g., like a variable).
Argument Passing

When using functions or subroutines, information (values, variables, etc.) is


typically passed to or from the routines. Argument association is a way of passing
values from actual arguments to formal arguments. If an actual argument is an
expression, it is evaluated and passed to the corresponding formal argument. If
an actual argument is a variable or constant, its value is passed to the
corresponding formal argument. There must be a one-to-one correspondence
between the actual argument (calling routine) and the formal argument
(function/subroutine).
The arguments in the call are matched up to the arguments in the
function/subroutine by position. Each of the arguments is matched by its
corresponding position. The names of the variables do not need to match,
however, the data types must match. For example, given the following subroutine
call and
subroutine,

each variable is matched by its corresponding position. Other variables in either


the calling routine (top) or the subroutine (bottom) are isolated from each
other. As such, the same variable name can be re-used in both the calling routine
and the subroutine (and refer to different values).

Functions

A function is a special type of Fortran subprogram that is expected to return a


single result or answer. A function will typically accept some kind of input
information and based on that information, return a result. The two types of
Fortran functions are described in the following sections.

Intrinsic Functions
As described previously, an intrinsic function is a built-in function that is already
available. Some of
the intrinsic functions already described include sin(), cos(), tan(), real(), int(),
and nint(). A more comprehensive list is contained in Appendix D.
User-Defined Functions

A user-defined function is a function that a written by the user for specific or


specialized requirements.
The general form of a user-defined function is as follows:
<type> function <name> ( <arguments> )
<declarations>
<body of function>
<name> = expression
return
end function <name>

The <type> is one of the Fortran data types: real, integer, logical, character, or
complex. It is possible to place the type declaration on a separate line from the
function statement. The information, in the form of arguments, is passed from the
calling routine to the function. Each of the passed arguments must be declared
and the declaration must include the type and the intent. The arguments in the
calling routine and the function must match and are matched up by position.

An example function to convert a Fahrenheit temperature to Celsius temperature


would be as follows:

real function fahr_to_celsius(ftemp)


real, intent(in) :: ftemp
fahr_to_celsius = (ftemp – 32.0) / 1.8
return
end function fahr_to_celsius

Which, given a Fahrenheit temperature, will return the Celsius temperature. The
single input argument, ftemp, is declared to be a real value and “intent(in)”, which
means that the value is expected to be coming into the function and cannot be
changed. The final value is returned to the calling routine by assigning a value to
the function name, fahr_to_celsius, in this example.

Subroutines

A subroutine is a Fortran subprogram that can accept some kind of input


information and based on that information, return a result or series of results.
The general form of a subroutine is a follows:
subroutine <name> ( <arguments> )
<declarations>
<body of subroutine>
return
end subroutine <name>

The information, in the form of arguments, is passed from the calling routine to
the subroutine. Each of the passed arguments must be declared and the declaration
must include the type and the intent. The arguments in the calling routine and the
subroutine must match and are matched up by position.

For example, given the following simple program to find the sum and average of
three numbers.

program subExample
implicit none
real :: x1=4.0, y1=5.0, z1=6.0, sum1, ave1
real :: x2=4.0, y2=5.0, z2=6.0, sum2, ave2
call sumAve(x1, y1, z1, sum1, ave1)
write (*,'(a,f5.1,3x,a,f5.1)') "Sum=", sum1, &
"Average=", ave1
call sumAve(x2, y2, z2, sum2, ave2)
write (*,'(a,f5.1,3x,a,f5.1)') "Sum=", sum2, &
"Average=", ave2
contains
subroutine sumAve (a, b, c, sm, av)
real, intent(in) :: a, b, c
real, intent(out) :: sm, av
sm = a + b + c
av = sm / 3.0
return
end subroutine sumAve
end program subExample

The arguments in the first call (x1, y1, z1, sum1, and ave1) are matched up to the
arguments in the subroutine (a, b, c, sm, and av) by position. That is, the x1 from
the call is matched with the a in the subroutine. The arguments in the second call
(x2, y2, z2, sum2, and ave2) are again matched up to the arguments in the
subroutine (a, b, c, sm, and av) by position. While the names of the variables do
not need to match, the data types must match. Variables declared in a function or
subroutine are not the same as variables in the calling routine. This is true, even
if they are the same name!

You might also like