XL Fortran for AIX 8.1

Language Reference


Function and Subroutine Subprograms

A subprogram is either a function or a subroutine, and is either an internal, external, or module subprogram. You can also specify a function in a statement function statement. An external subprogram is a program unit.



>>-subprogram_statement----------------------------------------><
 
 
>>-+--------------------+--------------------------------------><
   '-specification_part-'
 
 
>>-+----------------+------------------------------------------><
   '-execution_part-'
 
 
>>-+--------------------------+--------------------------------><
   '-internal_subprogram_part-'
 
 
>>-end_subprogram_statement------------------------------------><
 
 

subprogram_statement
See FUNCTION or SUBROUTINE for syntax details

specification_part
is a sequence of statements from the statement groups numbered (2), (3), and (4) in Order of Statements and Execution Sequence

execution_part
is a sequence of statements from the statement groups numbered (3) and (5) in Order of Statements and Execution Sequence, and which must begin with a statement from statement group (5)

internal_subprogram_part
See Internal Procedures for details

end_subprogram_statement
See END for syntax details on the END statement for functions and subroutines

An internal subprogram is declared after the CONTAINS statement in the main program, a module subprogram, or an external subprogram, but before the END statement of the host program. The name of an internal subprogram must not be defined in the specification section in the host scoping unit.

An external procedure has global scope with respect to the executable program. In the calling program unit, you can specify the interface to an external procedure in an interface block or you can define the external procedure name with the EXTERNAL attribute.

A subprogram can contain any statement except PROGRAM, BLOCK DATA and MODULE statements. An internal subprogram cannot contain an ENTRY statement or an internal subprogram.

Procedure References

There are two types of procedure references:

Function Reference

A function reference is used as a primary in an expression:



>>-function_name--(--+---------------------------+--)----------><
                     '-actual_argument_spec_list-'
 
 

Executing a function reference results in the following order of events:

  1. Actual arguments that are expressions are evaluated.
  2. Actual arguments are associated with their corresponding dummy arguments.
  3. Control transfers to the specified function.
  4. The function is executed.
  5. The value (or status or target, for pointer functions) of the function result variable is available to the referencing expression.

Execution of a function reference must not alter the value of any other data item within the statement in which the function reference appears. Invocation of a function reference in the logical expression of a logical IF statement or WHERE statement can affect entities in the statement that is executed when the value of the expression is true.

+-------------------------------IBM Extension--------------------------------+

The argument list built-in functions %VAL and %REF are supplied to aid interlanguage calls by allowing arguments to be passed by value and by reference, respectively. They can be specified in non-Fortran procedure references and in a subprogram statement in an interface body. (See %VAL and %REF.) See Statement Function and Recursion examples of function references.

+----------------------------End of IBM Extension----------------------------+

An allocatable function has an explicit interface.

On entry to an allocatable function, the allocation status of the result variable becomes not currently allocated

The function result variable may be allocated and deallocated any number of times during the execution of the function. However, it shall be currently allocated and have a defined value on exit from the function. Automatic deallocation of the result variable does not occur immediately on exit from the function, but instead occurs after execution of the statement in which the function reference occurs.

Examples of Subprograms and Procedure References

PROGRAM MAIN
REAL QUAD,X2,X1,X0,A,C3
QUAD=0; A=X1*X2
X2 = 2.0
X1 = SIN(4.5)                    ! Reference to intrinsic function
X0 = 1.0
CALL Q(X2,X1,X0,QUAD)            ! Reference to external subroutine
C3 = CUBE()                      ! Reference to internal function
CONTAINS
  REAL FUNCTION CUBE()           ! Internal function
    CUBE = A**3
  END FUNCTION CUBE
END
SUBROUTINE Q(A,B,C,QUAD)         ! External subroutine
  REAL A,B,C,QUAD
  QUAD = (-B + SQRT(B**2-4*A*C)) / (2*A)
END SUBROUTINE Q

Examples of Allocatable Function Results

FUNCTION INQUIRE_FILES_OPEN() RESULT(OPENED_STATUS)
  LOGICAL,ALLOCATABLE :: OPENED_STATUS(:)
  INTEGER I,J
  LOGICAL TEST
  DO I=1000,0,-1
    INQUIRE(UNIT=I,OPENED=TEST,ERR=100)
    IF (TEST) EXIT
100 CONTINUE
  END DO
  ALLOCATE(OPENED_STATUS(0:I))
  DO J=0,I
    INQUIRE(UNIT=J,OPENED=OPENED_STATUS(J))
  END DO
END FUNCTION INQUIRE_FILES_OPEN
 


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]