Functions and subprograms can be grouped into several categories;
- Intrinsic or "built-in" functions
- Statement or "one-line" functions (Note: This link is optional)
- Function subprograms
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
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
- The function name must follow the same rules as the names for PROGRAM statements. (<=6 char's, 1st is a letter)
- The arguments that appear in the function definition are called "dummy" or "formal" arguments. These can be thought of as "place holders" for the actual arguments which will be "passed" to it when the function is referenced. Arguments are separated by commas and enclosed by parentheses.
Actual arguments are the values of the real arguments used when the function is referenced in an executable statement.
It is important to remember that their names may be different, yet they must agree in total number and type.
- Just like in a main program, the variables in the argument list must be TYPED, this includes the FUNCTION NAME. Normal default naming conventions apply for integers/reals.
- Somewhere in the collection of executable statements, the FUNCTION NAME MUST BE GIVEN A VALUE.
- Just like intrinsic functions, a user-written FUNCTION returns a single value.
- The subprogram is terminated with an END statement. It is common practice to use a RETURN statement just before the END, but it is not required. A RETURN could also be placed elsewhere in the function which will cause the function to exit back to the calling program.
- FUNCTIONS contain:
- Opening documentation
- Comments scattered liberally throughout
- Any executable FORTRAN statement
- Even references to other functions/subroutines
- An example: a function that calculates the factorial of the input number, N.FUNCTION FACTRL (N) C This function computes n!, i.e. n*(n-1)*(n-2)*...*1 INTEGER FACTRL, N, I FACTRL = 1 IF ( N .GT. 1 ) THEN DO 10 I = 2, N FACTRL = FACTRL * I 10 CONTINUE END IF END
This function could immediately follow the main program which references it. A reference to this function in the main program might look like;: TERMA = FACTRL (4) (24 will be stored for TERMA) :Note that 4 is the ACTUAL ARGUMENT in this case. An INTEGER VARIABLE could also have been used.
IMPORTANT: ARGUMENTS MUST AGREE IN NUMBER AND TYPE.
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
- NAME is the name given to the subroutine and must follow the same rules as variables. However, NAME can not have a type. .
- The subroutines has a list of arguments which are optional (the parens are omitted if there are no arguments).
- In a subroutine, values are returned via the arguments. This means that if an argument is changed in the subroutine then the corresponding ACTUAL argument is changed in the CALLing program.
- Subroutines are more versatile than functions because they can modify arguments, use them without modification, or require no arguments at all.
- As in functions it is a general practice, but not required, to use a RETURN statement before the END. A RETURN statement elsewhere in the subroutine will return control back to the calling program.
- The subroutine is "called" as follows:CALL name (arguments)* IMPORTANT: The number and type of the arguments in the CALLing statement must agree with the number and type of the arguments in the SUBROUTINE.
- Some subroutine examples;With no arguments With input/output arguments SUBROUTINE MESSAG SUBROUTINE CONVER (R, TH, X, Y) REAL R, TH, X, Y PRINT*,'THIS MESSAGE' RETURN X = R * COS(TH) END Y = R * SIN(TH) RETURN ENDThe subroutine on the left has no arguments and simply prints out a message when it is called. The subroutine on the right uses the values for R, TH in polar coords and converts these into rectangular coords using the output variables X, Y.
- Separate sets of INPUT and OUTPUT variables are used quite often in subroutines. Typically, the INPUT values are listed first, and the OUTPUT arguments are last.
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)