• Aucun résultat trouvé

PROGRAM STATEMENT

Dans le document CRAY® COMPUTER SYSTEMS (Page 72-76)

LANGUAGE ELEMENTS AND STRUCTURE

PRINT *, VARIABLE END

2.3 PROGRAM UNITS

2.3.1 PROGRAM STATEMENT

Although the PROGRAM statement is optional, it is required for option f/F (Flowtrace; see 1.4.3); and option h/H (listing of all header statements;

see tables 1-1 and 1-2). When used, it is the first statement of the main program.

PROGRAM pgm [(h)]

pgm Symbolic name of the main program in which the PROGRAM statement appears; from one to eight alphanumeric characters. The name is global.

h Any sequence of allowed characters (see 2.1.1); has no effect on the executable program, but is allowed so that programs for other implementations of Fortran will run.

The ANSI Fortran Standard does not provide for the h field in the PROGRAM statement.

Example:

PROGRAM A1B2C3D4

PROGRAM X (INPUT,OUTPUT)

2.4 FUNCTIONS

A function is an executable entity invoked by a function reference (see 2.4.1) used as a primary in an expression. The reference becomes

associated with a value, called the function value. Example:

Y

=

FUN(X) + 2.0

The above statement calls function FUN to process the current value of X. The function reference FUN(X) takes on a new value, which is then used in the right-side expression to assign a new value to Y.

A function is one of the following:

A statement function is specified by a single statement within the program unit that uses the function (see 2.4.2).

An intrinsic function is provided by CFT77 and is always available unless an external function of the same name is substituted by use of the EXTERNAL statement, or unless its name is used for another entity (see 2.4.3).

An external function is specified by user-supplied code outside the calling program unit; if i t is written in Fortran i t is a

function subprogram. (Routines in other languages are not called

"subprograms" in ANSI terminology.) See 2.5.1.

2.4.1 FUNCTION REFERENCE

All functions use the same form of reference:

fun ( [a [ , a] ••• ] )

fun Symbolic name of a function or dummy procedure

a Actual argument; the parentheses are required even with no argument. Requirements for function arguments are discussed in 2.4.2, 2.4.3.1, and 2.6.2.

The types of actual arguments specified in a function reference must agree with associated dummy arguments defined in the called function. In

standard Fortran, the number of actual and dummy arguments must also

error or undefined value will result. To prevent this, any dummy argument not associated with an actual argument must follow, in the dummy argument list, all dummy arguments that are associated with actual arguments.

Examples:

Intrinsic:

User-specified:

SIN(T)

LOG (X**2 + Y**3 - 1.53) ATAN2(U,V)

MAX(I,J,K,L,M) FUNCTSP(D+A/1.414) STMTF(A,B)

Examples of function references within statements:

(1) Y

=

SIN(T)

(2) M

=

MIN«MOD(I,J)+3),14) - 7

Example 2 above shows nesting of functions and functions used within expressions.

2.4.1.1 Data type of a function: reference versus value

The name used to call a function has a type within the calling program unit. This type is distinct from the type of the value generated by the function. For valid results, these types must agree, but the agreement is not enforced in Fortran. The type of a function name is established in the same way as a variable's type, either implicitly or by means of a type or IMPLICIT statement (see 3.1). The type of the function value is

established in the code that executes the function. Example:

In the calling program unit:

INTEGER FUN, V, C Type declaration of function FUN V

=

FUN(C)

In the called function subprogram:

INTEGER FUNCTION FUN(X) INTEGER X

Header declares fnct as integer type X requires its own declaration

FUN

= ...

Function value is defined here; integer type The above subprogram generates a value whose type agrees with that of the name used to call the function.

Fortran does not check for type agreement between an external function reference and its function value; nor does it convert the types of function values, as is done for variables in expressions. A function value of the wrong type therefore causes invalid results. You are

responsible for agreement between external function references and their values. See 2.4.3.1 and 2.6.4.2 concerning intrinsic functions.

In addition to the requirements for type agreement, functions of type character also require agreement in the length of the character value.

The length of a character function reference must be declared by a previous CHARACTER or IMPLICIT statement (see examples in 3.7.1.2).

2.4.1.2 Execution of functions

Execution of a function reference results in the following actions.

1. Actual arguments that are expressions are evaluated.

2. Actual arguments are associated with dummy arguments.

3. The function is executed.

4. Control is returned to the calling program unit. The function value is then used in evaluating the expression that contains the

function reference.

Executing a function reference in a statement must not alter the value of any other entity within the same statement. Nor may it alter the value of any entity in a common block that affects the value of any other function reference in the same statement (see 4.6). Example:

FUNCTION X(PX) COMMONICIA

A=2 X= •••

END

FUNCTION Y(PY) COMMONICIA PY

=

A

Y= •••

END

PROGRAM P Z

=

X(Z)+Y(J)

END

!Illegal statement

If a function reference in a statement causes an actual argument of the function to be defined, that argument or any associated entities (see 4.5.5) must not appear elsewhere in the same statement. Example:

A(I)=F(I) Y=G(X)+X

The above statements, in which F and G are functions, produce unpredictable results when the reference to F defines I or the reference to G defines X.

The data type of an expression in which a function reference appears neither affects nor is affected by the evaluation of the actual arguments of the function.

2.4.1.3 Order of evaluation

The order of evaluation of mUltiple function references within a single statement is fixed only within a logical IF statement and within nested function references. In other statements that contain more than one

function reference, the value provided by each function reference must not be affected by the order in which the other function references are

evaluated.

Examples:

(1)

Dans le document CRAY® COMPUTER SYSTEMS (Page 72-76)