

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Haugli; Class: AEROSPAC ENG PROBLM; Subject: AEROSPACE ENGINEERING; University: Iowa State University; Term: Fall 2004;
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


D. Haugli, Lecturer Aer E 160 Aerospace Engineering 11/5/2004 Subroutine Subprograms Iowa State University
Subroutine Subprograms
Use. A subroutine is a subprogram that, like a function, can receive multiple input arguments,
but that unlike a function, can return more than one result. To access a subroutine, use a CALL statement, such as
CALL
(Note: functions may not be accessed with a CALL statement; subroutines must be accessed with
a call statement and may not be placed in an equation, write statement or condition.) Subroutines may be called by other subroutines, by functions and by main programs.
Arguments must be of the same type and in order by type. The total number of arguments must
be the same in the CALL and SUBROUTINE statements.
Example 1. Consider three real variables, a, b and c. Let these be the arguments of a subroutine named fire. If the CALL statement in the main program is,
CALL fire (a, b, c)
The corresponding subroutine statement might look like
SUBROUTINE fire(x)
where x is an array with three spaces,
REAL, INTENT (IN) :: x(3)
The result is that in the subroutine, x(1) receives the value of a, x(2) receives the value of b and x(3) receives the value of c.
The names of the arguments do not matter, and arrays and variables can be mixed. Programs and
subprograms will share memory when both use the same name for an argument; otherwise, each program unit (main program or subprogram) has its own memory.
Form. A subroutine has a form similar to any other FORTRAN program unit. The essential structure is outlined below. Arguments must be included both for variables coming into the subroutine from the calling program, and for values being returned to the calling program.
SUBROUTINE
<Insert declarations (CHARACTER, INTEGER, REAL, etc.)>
<Insert executable statement – this is the part of the code that tells the subroutine what to compute and how to compute it.>
RETURN END SUBROUTINE
D. Haugli, Lecturer Aer E 160 Aerospace Engineering 11/5/2004 Subroutine Subprograms Iowa State University
Arguments must be declared with either INTENT (IN), INTENT (OUT) or INTENT (IN
OUT). Arguments that are INTENT (IN) are passed in from the calling program unit, and are
treated as constants by the subroutine. The value of an INTENT (IN) argument cannot be
changed within the subroutine. Arguments that are INTENT (OUT) are calculated in the
subroutine and passed back to the calling program unit. A third option is to declare a variable as
INTENT (IN OUT), which brings in a value for the variable from the calling unit, allows the subroutine to change the value, and returns the new value back to the calling unit.
Example 2. Main program calls an internal subroutine that switches the values of two numbers. This example uses an INTENT (IN OUT) declaration in the subroutine.
PROGRAM submain IMPLICIT NONE REAL :: a, b
WRITE (,) "Enter two numbers, a and b." READ (,) a, b
CALL switch(a, b)
WRITE (,) "a = ", a WRITE (,) "b = ", b
STOP CONTAINS
SUBROUTINE switch (a, b) IMPLICIT NONE REAL, INTENT (IN OUT) :: a, b REAL :: temp
temp = b b = a a = temp
RETURN END SUBROUTINE switch
END PROGRAM submain