8.8.8.1 References to Generic Intrinsic Functions

The generic intrinsic function name COS lists six specific intrinsic functions that calculate cosines: COS, DCOS, QCOS, CCOS, CDCOS, and CQCOS. These functions return different values: REAL(4), REAL(8), REAL(16) (VMS, U*X), COMPLEX(4), COMPLEX(8), and COMPLEX(16) (VMS, U*X) respectively.

If you invoke the cosine function by using the generic name COS, the compiler selects the appropriate routine based on the arguments that you specify. For example, if the argument is REAL(4), COS is selected; if it is REAL(8), DCOS is selected; and if it is COMPLEX(4), CCOS is selected.

You can also explicitly refer to a particular routine. For example, you can invoke the double-precision cosine function by specifying DCOS.

Procedure selection occurs independently for each generic reference, so you can use a generic reference repeatedly in the same program unit to access different intrinsic procedures.

You cannot use generic function names to select intrinsic procedures if you use them as follows:

When an intrinsic function is passed as an actual argument to a procedure, its specific name must be used, and when called, its arguments must be scalar. Not all specific intrinsic functions can appear as actual arguments. (For more information, see Table 9-1.)

Generic procedure names are local to the program unit that refers to them, so they can be used for other purposes in other program units.

Normally, an intrinsic procedure name refers to the Fortran 90 library procedure with that name. However, the name can refer to a user-defined procedure when the name appears in an EXTERNAL statement.


Note: If you call an intrinsic procedure by using the wrong number of arguments or an incorrect argument type, the compiler assumes you are referring to an external procedure. For example, intrinsic procedure SIN requires one argument; if you specify two arguments, such as SIN(10,4), the compiler assumes SIN is external and not intrinsic.

Except when used in an EXTERNAL statement, intrinsic procedure names are local to the program unit that refers to them, so they can be used for other purposes in other program units. The data type of an intrinsic procedure does not change if you use an IMPLICIT statement to change the implied data type rules.

Intrinsic and user-defined procedures cannot have the same name if they appear in the same program unit.

Examples

Example 8-1 shows the local and global properties of an intrinsic function name. It uses intrinsic function SIN as follows:

Example 8-1 Using and Redefining an Intrinsic Function Name

    !    Compare ways of computing sine

         PROGRAM SINES
           DOUBLE PRECISION X, PI
           PARAMETER (PI=3.141592653589793238D0)
           COMMON V(3)

    !    Define SIN as a statement function 

           SIN(X) = COS(PI/2-X)
           DO X = -PI, PI, 2*PI/100

    !    Reference the statement function SIN 

             WRITE (6,100) X, V, SIN(X)
           END DO
           CALL COMPUT(X)
   100     FORMAT (5F10.7)
         END

         SUBROUTINE COMPUT(Y)
           DOUBLE PRECISION Y

    !    Use intrinsic function SIN as an actual argument 

           INTRINSIC SIN
           COMMON V(3)

    !    Define generic reference to double-precision sine 

           V(1) = SIN(Y)

    !    Use intrinsic function SIN as an actual argument 

           CALL SUB(REAL(Y),SIN)
         END

         SUBROUTINE SUB(A,S)

    !    Declare SIN as name of a user function 

           EXTERNAL SIN
    !    Declare SIN as type DOUBLE PRECISION 

           DOUBLE PRECISION SIN
           COMMON V(3)

    !    Evaluate intrinsic function SIN 

           V(2) = S(A)

    !    Evaluate user-defined SIN function 

           V(3) = SIN(A)
         END

   !     Define the user SIN function 

         DOUBLE PRECISION FUNCTION SIN(X)
           INTEGER FACTOR
           SIN = X - X**3/FACTOR(3) + X**5/FACTOR(5)     &
               - X**7/FACTOR(7)
         END

         INTEGER FUNCTION FACTOR(N)
           FACTOR = 1
           DO I=N,1,-1
             FACTOR = FACTOR * I
           END DO
         END

The statement function named SIN is defined in terms of the generic function name COS. Because the argument of COS is double precision, the double-precision cosine function is evaluated. The statement function SIN is itself single precision.

The statement function SIN is called.

The name SIN is declared intrinsic so that the single- precision intrinsic sine function can be passed as an actual argument at 5.

The generic function name SIN is used to refer to the double-precision sine function.

The single-precision intrinsic sine function is used as an actual argument.

The name SIN is declared a user-defined function name.

The type of SIN is declared double precision.

The single-precision sine function passed at 5 is evaluated.

The user-defined SIN function is evaluated.

The user-defined SIN function is defined as a simple Taylor series using a user-defined function FACTOR to compute the factorial function.

For More Information:


Previous Page Next Page Table of Contents