1 Lecture 8 Function Subprograms In mathematics and engineering science, we quite often need to define, use and evaluate a function of one or more variables. For example, we might have the functions f ( x) or g ( x, y ) etc. The arguments of the functions might be scalar or vector quantities. In Fortran, we can define a function to be a special program, sometimes called a subprogram or subroutine, which acts very similar to functions in mathematics. For example, suppose we have the function: f ( x, y) x 2 3xy sin 2 ( x /(1 y 3 )) We can define this function in Fortran with the following function subprogram: 2 Lecture 8 REAL FUNCTION F (X,Y) REAL :: X,Y F = X**2 – 3*X*Y +(SIN(X/(1.+Y**3)))**2 END We can then use the function in a Fortran program by using its name (in this case F) in any statement where we can normally use a variable name (e.g., in an assignment statement). For example, suppose we need the value of the function for values of x from 0 to1 and y from 0 to 1 in steps of 0.1. We could write a main program followed by a function subprogram as shown below. We will use what is called an "external subprogram" for the function since the function subprogram is located external to the main program (i.e., it is placed after the END statement of main program). 3 Lecture 8 PROGRAM Evaluate_function REAL :: X,Y,F DO I =0,10 DO J=0,10 X=I*0.1 Y=J*0.1 A=F(X,Y) rest of program statements END DO END DO END PROGRAM Evaluate_function REAL FUNCTION F (X,Y) REAL :: X,Y F = X**2 – 3*X*Y +(SIN(X/(1.+Y**3)))**2 END FUNCTION F Note that the function is an external function that is placed outside of PROGRAM Evaluate_function (i.e., it is placed after the END 4 Lecture 8 PROGRAM statement). The external function (and its resulting value) can be used just like a variable name. For example, all of the following a valid uses: IF (F(X,Y) .LE. 0.) EXIT A = 27.3/F(X,Y)**2 PRINT *, F(X,Y) The arguments in a function subprogram are sometimes called "dummy" arguments because they are replaced by the values placed in the argument list when needed by another program. In other words, the necessary values of all arguments are passed from the main program to the function subprogram when the function is to be evaluated. The following could have been used: REAL FUNCTION F (A,B) REAL :: A,B F = A**2 – 3*A*B +(SIN(A/(1.+B**3)))**2 END 5 Lecture 8 The general form of a function subprogram is given by FUNCTION function-name (argument-list) specification statements (if any) executatble statements (if any) function-name = expression more executable statements (if needed) END The FUNCTION statement may also be used to "type" the function: type-idendifier FUNCTION function-name (argument-list) The END statement may also be written END FUNCTION function-name There must be at least one of the statements function-name = expression in each functin subprogram. A RETURN statement may be placed anywehre in the subprogram if program logic dictates that execution should be returned to the calling program (otherwise this occurs when the END statement is executed. Lecture 8 6 When execution is passed to a function subprogram (i.e., the function is "called" to be evaluated), several things are important: Arguments of the function subprogram take on the values that are passed to it from the program from which the function is referenced. The list of arguments (in both the program using the function and the function subprogram) must exactly in type of variable/constant, number of arguments and order of arguments. Execution of the function program continues until the END statement is reached; or a RETURN statement is reached. The RETURN statement is an executable statement that may be used to return to the calling program (before reaching the END statement). Variables in a subprogram (except those in the argument list) are general considered to be local variables, i.e., any assignment of a value to the variable within the subprogram has no effect outside the subprogram. These local variables are not available for use outside the subprogram. However, variables in the argument list are not generally local and, if the variable is changed in the subprogram, the new value applies to the calling program (so these would be called global varialbes). This is 7 Lecture 8 because the calling program passes the address of the variable in the argument list to the function. Note: this is the pre-Fortran 90 rule for external subprograms. In Fortran 90 where subprograms may be internal to the main program (see text), variables are considered to be global unless changed by the INTENT statement. The local/global nature of variables in the argument list of a subprogram may be changed by the INTENT statement. Its general form is Type-specifier, INTENT(usage-attribute) variable-list where usage-attribute is either IN, OUT or INOUT. IN means the value of a variable in the argument list will not be altered during execution of the subprogram (the variable is used for input to the subprogram only). Hence, the variable is considered INput to the subroutine. OUT means the variable is intended to pass values back to the calling program. Hence, the variable is considered OUTput from the subroutine back to the calling program Lecture 8 8 INOUT means the variable can be used to pass information both to and from the subroutine. In Fortran 90, function subprograms can be made accessible to the main program in three ways: 1. As an external subprogram (the type we have discussed here). The function subprogram is placed after the END PROGRAM statement of the main program. 2. As in internal subprogram. The function subprogram is placed in the main program just before the END PROGRAM statement and after the CONTAINS statement. The general form is given by: PROGRAM Main main program statements CONTAINS subprogram #1 more subprograms as needed END PROGRAM Main Lecture 8 3. As a module subprogram. The function subprogram is placed in a module from which it can be imported into the main program. Most programmers use external subprograms. See Chapter 6 for a description of internal and module subprograms. 9