76
Section 2: Compiler
TI
-
89 / TI
-
92 Plus Sierra C Assembler Reference Manual
Not for Distribution
Beta Version February 2, 2001
The following is an example of an old-style function definition:
int f5( a, b, c, d, e )
int a;
short b;
long c;
float d;
double e;
{
/* function body */
}
The function f5 is defined to accept five arguments and return a value of type
int
.
The formal parameter type declarations
int a
,
short b
,
long c
,
float d
, and
double e
declare how the parameters will be used inside the function. They do
not describe the types of the actual parameters that are pushed onto the stack at
the function call site. The expected types of the actual parameters are
determined by the integral and floating-point promotion rules. The promotion
rules state that arguments of type
char
and
short
are converted to type
int
, and
that arguments of type
float
are converted to type
double
; no other conversions
are performed.
At a function call site, there is no information available on the types of the
arguments. If a called function has a formal parameter declared to be of type
float
, it is expecting an actual parameter of type
double
to have been pushed on
the stack. If, however, the parameter is of type
int
, it must be explicitly cast to
either a
float
or a
double
at the call site to prevent it from being incorrectly
pushed as an
int
. Finally, if the function is called with too few or too many
parameters, the error will go undetected during compilation.
2.11.1.3. Mixing Prototype and Old-Style Declarations
The Sierra C compiler supports both new-style (prototype) and old-style
(nonprototype) function declarations and definitions as specified by the ANSI C
standard. However, it is highly recommended that the prototype style be used
exclusively.
Mixing prototype declarations and old-style function definitions should be avoided
because it will often create problems. The following is an example of a prototype
declaration and an old-style function definition:
int func( short a );
int func( a )
short a;
{
/* function body */
}