# 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), COMPLEX(4), COMPLEX(8), and COMPLEX(16) 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:

- The name of a statement function

- A dummy argument name, a common block name, or a variable or array name

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 Specific Functions Not Allowed as Actual Arguments in Intrinsic Procedures.)

A reference to a generic intrinsic procedure name in a program unit does not prevent use of the name for other purposes elsewhere in the program.

Normally, an intrinsic procedure name refers to the Fortran 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.

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

The following example shows the local and global properties of an intrinsic function name. It uses the name SIN in different procedures as follows:

- The name of a statement function

- The generic name of an intrinsic function

- The specific name of an intrinsic function

- The name of a user-defined function

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)
            print *
            print *, "                    Way of computing SIN(X)"
            print *
            print *, "      X       Statement   Intrinsic   Intrinsic     User's "
            print *, "              function      DSIN       SIN as arg     SIN  "
            print *
            DO X = -PI, PI, PI/2
                        CALL COMPUT(X)
    !    References the statement function SIN
                        WRITE (6,100) X, SIN(X), V
             END DO
    100      FORMAT (5F12.7)
           END

           SUBROUTINE COMPUT(Y)
             DOUBLE PRECISION Y

    !    Use intrinsic function SIN - double-precision DSIN will be passed
    !       as an actual argument
             INTRINSIC SIN
             COMMON V(3)

    !    Makes the generic name SIN reference the double-precision sine DSIN
             V(1) = SIN(Y)

    !    Use intrinsic function SIN as an actual argument - will pass DSIN
              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 passed as the dummy argument
               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

    !    Compute the factorial of N
           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.

Parent topic: References to Generic Procedures

## See Also

EXTERNAL attribute
INTRINSIC attribute
Intrinsic procedures
Names for details on the scope of names