Example 6-1 shows the different ways to use intrinsic function names. In this annotated example, a single executable program uses the name SIN in four distinct ways:
Using the name in these four ways emphasizes the local and global properties of the name.
C Compare ways of computing sine PROGRAM SINES REAL*8 X, PI PARAMETER (PI=3.141592653589793238D0) COMMON V(3) C Define SIN as a statement function SIN(X) = COS(PI/2-X) DO 10 X = -PI, PI, 2*PI/100 CALL COMPUT(X) C Reference the statement function SIN 10 WRITE (6,100) X, V, SIN(X) 100 FORMAT (5F10.7) END SUBROUTINE COMPUT(Y) REAL*8 Y C Use intrinsic function SIN as actual argument INTRINSIC SIN COMMON V(3) C Define generic reference to double-precision sine V(1) = SIN(Y) C Use intrinsic function SIN as actual argument CALL SUB(REAL(Y),SIN) END SUBROUTINE SUB(A,S) C Declare SIN as name of user function EXTERNAL SIN
C Declare SIN as type REAL*8 REAL*8 SIN COMMON V(3) C Evaluate intrinsic function SIN V(2) = S(A) C Evaluate user-defined SIN function V(3) = SIN(A) END C Define the user SIN function REAL*8 FUNCTION SIN(X) INTEGER FACTOR SIN = X - X**3/FACTOR(3) + X**5/FACTOR(5) 1 - X**7/FACTOR(7) END INTEGER FUNCTION FACTOR(N) FACTOR = 1 DO 10 I=N,1,-1 10 FACTOR = FACTOR * I END
A 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.