SUBPROGRAMS

TYPES OF FUNCTIONS AND SUBPROGRAMS

Functions and subprograms can be grouped into several categories;

INTRINSIC FUNCTIONS

The intrinsic functions are the set of "built-in" or library functions that all versions of FORTRAN provide. These are such things as SIN, COS, EXP,...

The user of these functions "passes" one or more arguments to the function.

e.g., Y = COS(0.0) ABMX = MAX (A,B)
0.0, A and B are called "arguments"

List of Intrinsic Fortran functions

More on how to use intrinsic functions

FUNCTION SUBPROGRAMS

A FUNCTION subprogram is a "mini-program", that is, a collection of program statements which make it look like a program.

Functions, being separate entities, perform separate, specific tasks.

Functions are defined outside of the main program. They can either be written after the END of the main program or in another file altogether. Since they are separate from the main program they can be used again by other programs by simply attaching them to the other program or putting them in a library.

The general form of a FUNCTION looks like;

          FUNCTION name (arguments)
          declarations
          subprogram statements
          RETURN
          END

SUBROUTINES

Subroutines are very much like FUNCTIONS in that they are independent program units or modules, but they differ from functions in several important ways.

1. Functions return a single value to the program that references them whereas SUBROUTINES may return more than one value, or none at all.

2. Functions return values via the function name; subroutines return values via arguments.

3. A functions is referenced by using its name in an expression, whereas a subroutine is referenced by a CALL statement.

The general form of a SUBROUTINE is similar to that of a FUNCTION (and thus other program units);

          SUBROUTINE name (arguments)
          declarations
          subprogram statements
          RETURN
          END
PASSING ARGUMENTS

Actual arguments are "passed" to the subprograms and used in place of the formal arguments. As previously stated, the actual arguments may have different names than the formal arguments BUT they must agree in NUMBER and TYPE.

Simple variables are the easiest to pass, but there are several implications to consider.

* Note that you can use an expression as an actual argument. The expression will be evaluated before passing and the type must agree with the formal argument.

Arrays are passed by using the array's name. There are a few notes to consider.

* The corresponding array argument in the subroutine must be dimensioned no larger than the actual array in the main program.

* Note that an array element is handled the same as a simple variable. i.e, ALPHA(4) refers to one simple variable.

* Arrays may have "adjustable" dimensions, that is you may dimension an array in a subroutine based on the value of one of the arguments.

          SUBROUTINE STUFF (AX, BX, N)
          REAL AX(N), BX(N)
          :
Here's how the main program might look in this case:
C  PROGRAM COMPUTE
      REAL X(100), Y(100)
C read in some data.  Check to make sure n is less than or equal to 100.
      open(1,file='xy.data')
      READ(1,*) NPTS
      IF (N .GT. 100) THEN
        write(6,*) ' N too large for program.  Make dimensions bigger and try again' 
        stop
      ELSE  
        DO 100 I=1,NPTS    
          READ(1,*) X(I), Y(I)
  100   CONTINUE
      END IF
C
C CAll subroutine STUFF to do some "stuff":
      CALL STUFF(X,Y,NPTS)
           . 
           .
           .
      END
      SUBROUTINE STUFF(AX,BX,N)
      REAL AX(N), BX(N)
         :
C Subroutine STUFF starts directly after the END statement of the main program.
C

Your subprograms can either directly follow your main program in one file, or may be exist as separate files and be compiled separately and then linked. Click here for an example of how to do this. (optional)