⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.gml

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 GML
📖 第 1 页 / 共 2 页
字号:
.ix 'function' 'definition'
.pp
There are two forms for defining a function. The first form is,
.cillust begin
storage-class return-type identifier
.mono (
parameter-type-list
.mono )
.cbr
.mono {
.cbr
&SYSRB.&SYSRB.&SYSRB.&SYSRB.declaration-list
.csk
&SYSRB.&SYSRB.&SYSRB.&SYSRB.statement-list
.cbr
.mono }
.cillust end
.pc
The
.us storage-class
may be one of
.kw extern
or
.kw static
..ct ..li .
If
.us storage-class
is omitted,
.kw extern
is assumed.
.pp
The
.us return-type
may be any valid type except an
.us array
..ct ..li .
If
.us return-type
is omitted,
.kw int
is assumed.
.pp
The
.us identifier
is the name of the function.
.pp
The
.us parameter-type-list
is either
.kw void
or empty,
meaning the function takes no parameters, or a
comma-separated list of declarations of the objects, including both
type and parameter name (identifier).
If multiple arguments of the same type are specified, the type of
each argument must be given individually. The form,
.uillust type id1, id2
is not permitted within the parameter list.
.pp
If the
.us parameter-type-list
ends with
.mono ,...
then the function will accept a variable number of
arguments.
.pp
Any parameter declared as "array of
.us type
..ct "
is changed to
"pointer to
.us type
..ct ".
Any parameter declared as "
.us function
..ct "
is changed to
"pointer to
.us function
..ct ".
.keep begin
.pp
The following examples illustrate several function definitions:
.millust int F( void )
.discuss begin
The function
.mono F
has no parameters, and returns an integer.
.discuss end
.keep break
.millust void G( int x )
.discuss begin
The function
.mono G
has one parameter, an integer object named
.mono x
..ct ,
and does not return a value.
.discuss end
.keep break
.millust void * H( long int len, long int wid )
.discuss begin
The function
.mono H
has two parameters, long integer objects named
.mono len
and
.mono wid
..ct ,
and returns a pointer which does not point to any particular type
of object.
.discuss end
.keep break
.millust void I( char * format, ... )
.discuss begin
The function
.mono I
has one known parameter, an object named
.mono format
that is a pointer to a character (string).
The function also accepts a variable
number of parameters following
.mono format
..ct ..li .
The function does not return a result.
.discuss end
.keep end
.pp
This form of function definition also serves as a prototype declaration
for any calls to the function that occur later in the same
module. With the function prototype in scope at the time of a call to the
function, the arguments are converted to the type of the corresponding
parameter prior to the value being assigned. If a call to the function
is to be made prior to its definition, or from another module, a
function prototype should be specified for it in order to ensure proper
conversion of argument types.
Failure to do this will result in the
.ix 'default argument promotion'
default argument promotions being
performed, with undefined behavior if the function parameter types
do not match the promoted argument types.
.keep begin
.pp
The second form of function definition is,
.cillust begin
storage-class return-type identifier
.mono (
identifier-list
.mono )
.cbr
&SYSRB.&SYSRB.&SYSRB.&SYSRB.declaration-list
.cbr
.mono {
.cbr
&SYSRB.&SYSRB.&SYSRB.&SYSRB.declaration-list
.csk
&SYSRB.&SYSRB.&SYSRB.&SYSRB.statement-list
.cbr
.mono }
.cillust end
.keep end
.pc
The
.us storage-class
..ct ,
.us return-type
and
.us identifier
parts are all the same as for the first form of definition.
In this form, the
.us identifier-list
is a (possibly empty) comma-separated list of identifiers (object
names) without any type information. Following the closing parenthesis,
and before the opening brace of the body of the function,
the declarations for the objects are given, using the normal rules.
Any object of type
.kw int
need not be explicitly declared.
.pp
In the declarations of the parameter identifiers,
.kw register
is the only storage-class specifier that may be used.
.pp
A function prototype is created from the definition
after the
.ix 'default argument promotion'
default argument promotions have been performed on each
parameter.
All arguments to a function declared in this manner will have the
default argument promotions performed on them.
The resulting types
must match the types of the declared parameters, after promotion.
Otherwise,
the behavior is undefined.
.pp
Note that it is impossible to pass an object of type
.kw float
to a function declared in this manner. The argument of type
.kw float
will automatically be promoted to
.kw double
..ct ,
and the parameter will also be promoted to
.kw double
(assuming that it was declared as
.kw float
..ct ..li ).
For similar reasons, it is not possible to pass an object of type
.kw char
or
.kw short int
without promotion taking place.
.pp
According to the ISO standard for the C language,
.bd this form of function definition is obsolete
and should not be used. It is provided for historical reasons,
in particular, for compatibility with older C compilers.
Using the first form of function definition often allows the compiler
to generate better code.
.keep begin
.pp
The following examples are the same as those
given with the first form above,
with the appropriate modifications:
.millust int F()
.discuss begin
The function
.mono F
has no parameters, and returns an integer.
.discuss end
.keep break
.millust void G( x )
.discuss begin
The function
.mono G
has one parameter, an integer object named
.mono x
..ct ,
and does not return a value.
This example could have also been written as,
.millust begin
void G( x )
    int x;
.millust end
.keep break
.pc
which explicitly declares
.mono x
to be an integer.
.discuss end
.keep break
.millust begin
void * H( len, wid )
    long int len;
    long int wid;
.millust end
.discuss begin
The function
.mono H
has two parameters, both integer objects named
.mono len
and
.mono wid
..ct ,
and returns a pointer which does not point to any particular type
of object.
Any call to this function must ensure that the arguments are long
integers, either by using an object so declared, or by explicitly
casting the object to the type.
.discuss end
.keep end
.pp
The last example using the ellipsis (
..ct .mono ,...
..ct )
notation is not
directly representable using the second form of function definition.
With most compilers it is possible to handle variable argument lists
in this form,
but knowledge of the mechanism used to pass arguments to functions
is required, and this mechanism may vary between different compilers.
.*
.section The Body of the Function
.*
.pp
Following the declaration of the function
and the opening brace
is the
.us body
of the function.
It consists of two portions, both of which are optional.
.pp
The first portion is the declaration list for any objects needed within
the function. These objects may have any type and any storage class.
Objects with storage class
.kw register
or
.kw auto
have
.ix 'automatic storage duration'
.ix 'storage duration' 'automatic'
.us automatic storage duration
..ct ,
meaning they are created when the function is called, and destroyed
when the function returns to the caller.
(The value of the object is not preserved between calls
to the function.)
Objects with storage class
.kw extern
or
.kw static
have
.ix 'static storage duration'
.ix 'storage duration' 'static'
.us static storage duration
..ct ,
meaning they are created once, before the function is ever called, and
destroyed only when the program terminates. Any value placed in such
an object will remain even after the function has returned, so that
the next time the function is called the value will still be present
(unless some other action is taken to change it, such as using
another object containing a pointer to the static object
to modify the value).
.pp
Unless an explicit
.kw return
statement is executed, the function
will not return to the caller until the brace at the end of the
function definition is encountered.
The return will be as if a
.kw return
statement with no expression was executed.
If the function is declared as returning a value, and the caller
attempts to use the value returned in this manner, the behavior
is undefined.
The value used will be arbitrary.
.pp
A function may call itself
.ix 'recursion'
.ix 'function' 'recursion'
.us :HP0.(:eHP0.recursion:HP0.):eHP0.
directly, or it may call another function or functions which in turn
call it. Any objects declared with
.us automatic storage duration
are created as a new instance of the object upon each recursion, while
objects declared with
.us static storage duration
only have one instance shared between the recursive instances of the
function.
.*
.section Function Prototypes
.*
.ix 'function' 'prototype'
.ix prototype function
.pp
A function prototype is like a definition of a function, but without
the body.
A semi-colon is specified immediately following the closing
right parenthesis of the function's declaration.
The prototype describes the name of the function, the
types of parameters it expects (names are optional)
and the type of the return value.
This information can be used by the C compiler to do proper argument
type checking and
conversion for calls to the function, and to properly handle the
return value.
.pp
If no function prototype has been found by the time a call to a function
is made, all arguments have the default argument promotions performed
on them, and the return type is assumed to be
.kw int
..ct ..li .
If the actual definition of the function does not have parameters that
match the promoted types, the behavior is undefined.
If the return type is not

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -