📄 fsubp.gml
字号:
.chap *refid=fsubp Functions and Subroutines
.*
.if &e'&dohelp eq 0 .do begin
.section Introduction
.do end
.*
.np
Functions and subroutines are procedures that fall into one of the
following categories.
.autopoint
.point
Statement functions
.point
Intrinsic functions
.point
External functions
.point
Subroutines
.endpoint
.pc
First let us introduce some terminology.
.np
.ix 'program unit'
A
.us program unit
is a collection of &product statements and comments that can be
either a main program or a subprogram.
.np
.ix 'main program'
A
.us main program
identifies the program unit where execution is to begin.
A main program is a program unit which has as its first statement a
.kw PROGRAM
statement or one which does not have a
.kw PROGRAM,
.kw FUNCTION,
.kw SUBROUTINE
or
.kw BLOCK DATA
statement as its first statement.
Complete execution of the main program implies the complete execution
of the program.
Each executable program can contain only one main program.
.np
.ix subprogram
A
.us subprogram
is a program unit that either has a
.kw FUNCTION,
.kw SUBROUTINE
or
.kw BLOCK DATA
statement as its first statement.
This chapter will only deal with subprograms that have a
.kw FUNCTION
or
.kw SUBROUTINE
statement as its first statement.
.*
.section Statement Functions
.*
.np
A statement function is a procedure defined by a single statement.
Its definition must follow all specification statements and precede
the first executable statement.
The statement defining a statement function is not an executable
statement.
.np
.ix 'statement function'
.ix function statement
A
.us statement function
has the following form.
.mbox begin
sf ( [d [,d] ...] ) = e
.mbox end
.synote
.mnote sf
is the name of the statement function.
.mnote d
is a statement function dummy argument.
.mnote e
is an expression.
.endnote
.pc
The expression
.id e
and the statement function name
.id sf
must conform according to the rules of assignment as described in the
chapter entitled :HDREF refid='fassmnt'..
.np
The statement function dummy arguments are variable names
and are used to indicate the order, number and type of the arguments of
the statement function.
A dummy argument name of a statement function must only appear once
in the dummy argument list of the statement function.
Its scope is the statement defining the statement function.
That is, it becomes defined when the statement function is
referenced and undefined when execution of the statement function
is completed.
A name that is a statement function dummy argument can also be the
name of a variable, a common block, the dummy argument of another
statement function or appear in the dummy argument list of a
.kw FUNCTION,
.kw SUBROUTINE
or
.kw ENTRY
statement.
It cannot be used in any other context.
.np
The expression
.id e
can contain any of the following as operands.
.autopoint
.point
A constant.
.point
A symbolic constant.
.point
A variable reference.
This can be a reference to a statement function dummy argument or to a
variable that appears within the same program unit which defines the
statement function.
If the statement function dummy argument has the same name as
a variable in the same program unit, the statement function dummy
argument is used.
The variable reference can also be a dummy argument that appears in the
dummy argument list of a
.kw FUNCTION
or
.kw SUBROUTINE
statement.
If it is a dummy argument that has appeared in the dummy argument list
of an
.kw ENTRY
statement, then the
.kw ENTRY
statement must have previously appeared.
.point
An array element reference.
.point
An intrinsic function reference.
.point
A reference to a statement function whose defining statement has
previously appeared.
.point
An external function reference.
.point
A dummy procedure reference.
.point
An expression enclosed in parentheses which adheres to the rules
specified for the expression
.id e.
.endpoint
.beglevel
.*
.section Referencing a Statement Function
.*
.np
A statement function is referenced by its use in an expression.
The process of executing a statement function involves the following
steps.
.autopoint
.point
The expressions that form the actual arguments to the statement function
are evaluated.
.point
The dummy arguments of the statement function are associated with the
actual arguments.
.point
The expression
.id e
is evaluated.
.point
The value of the result is converted to the type of the statement
function according to the rules of assignment and is available
to the expression that contained the reference to the statement function.
.endpoint
.np
The actual arguments must agree in order, number and type with the
corresponding dummy arguments.
.cp 17
.exam begin
SUBROUTINE CALC( U, V )
REAL POLY, X, Y, U, V, Z, CONST
*
* Define a Statement Function.
*
POLY(X,Y) = X**2 + Y**2 + 2.0*X*Y + CONST
*
* Invoke the Statement Function.
*
CONST = 23.5
Z = POLY( U, V )
PRINT *, Z
END
.exam end
.pc
In the previous example, note that after the execution of the statement
function, the values of
.id X
and
.id Y
are not equal to the value of
.id U
and
.id V
respectively; they are undefined.
.*
.section Statement Function Restrictions
.*
.autonote
.note
A statement function is local to the program unit in which it is defined.
Thus, a statement function name is not allowed to appear in an
.kw EXTERNAL
statement
and cannot be passed to another procedure as an actual argument.
The following example illegally attempts to pass the statement function
.id F
to the subroutine
.id SAM.
.cp 16
.exam begin
* Illegally passing a statement function
* to a subroutine.
PROGRAM MAIN
F(X) = X
.
.
.
CALL SAM( F )
.
.
.
END
.exam end
.note
If a statement function
.id F
contains a reference to another statement function
.id G,
then the statement defining
.id G
must have previously appeared.
In the following example, the expression defining the statement function
.id F
illegally references a statement function
.id G
whose defining statement follows
the statement defining
.id F.
.cp 13
.exam begin
* Illegal order of statement functions.
.
.
.
F(X) = X + G(X)
G(X) = X + 2
.
.
.
.exam end
.note
The statement function name must not be the same name of any other
entity in the program unit except possibly the name of a common block.
.note
If a dummy argument of a statement function is of type CHARACTER, then
its length specification must be an integer constant expression.
The following is illegal.
.cp 11
.exam begin
SUBROUTINE SAM( X )
CHARACTER*(*) X
* Illegal - CHARACTER*(*) dummy argument not
* allowed in statement function.
F(X) = X
PRINT *, F('ABC')
END
.exam end
.note
An actual argument to a statement function can be any expression, except
character expressions involving the
concatenation of an operand whose length specification is
.mono (*)
unless the operand is a symbolic constant.
.endnote
.endlevel
.*
.section Intrinsic Functions
.*
.np
.ix 'intrinsic function'
.ix function intrinsic
An
.us intrinsic function
is a function that is provided by &product..
.beglevel
.*
.section Specific Names and Generic Names of Intrinsic Functions
.*
.np
.ix 'generic name'
.ix 'specific name'
All intrinsic functions can be referenced by using the
.us generic name
or the
.us specific name
of the intrinsic function.
The specific name uniquely identifies the function to be performed.
The type of the result is predefined thus its name need not appear
in a type statement.
For example, CLOG is a specific name of the generic LOG function and
computes the natural logarithm of a complex number.
The type of the result is also COMPLEX.
.np
When the generic name is used, a specific name is selected based
on the data type of the actual argument.
For example, the generic name of the natural logarithm intrinsic
function is LOG.
To compute the natural logarithm of REAL, DOUBLE PRECISION, COMPLEX or
DOUBLE PRECISION COMPLEX data, the generic name LOG can be used.
Generic names simplify the use of intrinsic functions because the same
name can be used with more than one type of argument.
.autonote Notes:
.setptnt 0 5
.note
It is also possible to pass intrinsic functions to subprograms.
When doing so, only the specific name of the intrinsic function can
be used as an actual argument.
The specific name must have appeared in an
.kw INTRINSIC
statement.
.note
If an intrinsic function has more than one argument, each argument must
be of the same type.
.note
The generic and specific name of an intrinsic function is the same
for some intrinsic functions.
For example, the specific name of the intrinsic function which computes
the sine of an argument whose type is REAL is called SIN which is also
the generic name of the sine function.
.endnote
.if &e'&dohelp eq 0 .do begin
.im fitabmon
.do end
.el .do begin
.im fitabmon
.do end
.endlevel
.*
.section External Functions
.*
.np
.ix 'external function'
.ix function external
An
.us external function
is a program unit that has a
.kw FUNCTION
statement as its first statement.
It is defined externally to the program units that reference it.
The form of a
.kw FUNCTION
statement is defined in the chapter entitled :HDREF refid='fstats'..
.np
The name of an external function is treated as if it was a variable.
It is through the function name that the result of an
external function becomes defined.
This variable must become defined before the execution of the external
function is completed.
Once defined, it can be referenced or redefined.
The value of this variable when a
.kw RETURN
or
.kw END
statement is executed is the result returned by the external function.
.cp 11
.exam begin
INTEGER FUNCTION VECSUM( A, N )
INTEGER A(N), I
VECSUM = 0
DO 10 I = 1, N
VECSUM = VECSUM + A(I)
10 CONTINUE
END
.exam end
.np
If the variable representing the return value of the external function
is of type CHARACTER with a length specification of
.mono (*),
it must not be the operand of a concatenation operator unless it
appears in a character assignment statement.
.np
It is also possible for an external function to return results
through its dummy arguments by assigning to them.
The following example demonstrates this.
.cp 35
.exam begin
INTEGER MARKS(40), N
REAL AVG, STDDEV, MEAN
PRINT *, 'Enter number of marks'
READ( 5, * ) N
PRINT *, 'Enter marks'
READ( 5, * ) (MARKS(I), I = 1, N)
AVG = MEAN( MARKS, N, STDDEV )
PRINT *, 'Mean = ', AVG,
$ ' Standard Deviation = ', STDDEV
END
*
* Define function MEAN to return the average by
* defining the function name and return the standard
* deviation by defining a dummy argument.
*
REAL FUNCTION MEAN( A, N, STDDEV )
INTEGER A, N, I
REAL STDDEV
DIMENSION A(N)
MEAN = 0
DO 10 I = 1, N
MEAN = MEAN + A(I)
10 CONTINUE
MEAN = MEAN / N
STDDEV = 0
DO 20 I = 1, N
STDDEV = STDDEV + ( A(I) - MEAN )**2
20 CONTINUE
STDDEV = SQRT( STDDEV / (N - 1) )
END
.exam end
.beglevel
.*
.section Referencing an External Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -