int main()
{
int x, y;
x = 4;
y = SomeFunction(x);
}
|
Question:
|
Answer:
|
|
|
|
[extern] return-type FuncName ( param1_type [var1], param2_type [var2], .... ) ;
|
Example:
extern int f( int ) ;
char SomeFunc( double, int, float ) ;
double sin( double x ) ;
|
|
|
The function prototype provides information to the C compiler on whether it needs to perform conversions.
int main( int argc, char *argv[] )
{
double a, b;
a = 4.0;
b = square( a ); // <---- Function definition is not available
// when C compiler translates this
// function call....
}
double square( double x )
{
double r; // Define a local variable
r = x * x; // Statement
return ( r ); // Return statement
}
|
double square( double ); // Function prototype (function declaration)
int main( int argc, char *argv[] )
{
double a, b;
a = 4.0;
b = square( a ); // <---- Function definition is NOW available !!!
}
double square( double x )
{
double r; // Define a local variable
r = x * x; // Statement
return ( r ); // Return statement
}
|
How to run the program:
|
|
Example: do not do the following
int square( double ); // BAD declaration for the square( ) function !!!
int main( int argc, char *argv[] )
{
double a, b;
a = 4.0;
b = square( a ); // Make assumption: int square(int)
printf("a = %lf, b (square) = %lf\n", a, b);
}
double square( double x )
{
double r; // Define a local variable
r = x * x; // Statement
return ( r ); // Return statement
}
|
Result:
cs255@aruba (6608)> gcc func1x-bad.c func1x-bad.c:18: error: conflicting types for 'square' func1x-bad.c:4: note: previous declaration of 'square' was here |
Demo:
cd /home/cs255000/demo/c/set1 gcc func1x-bad.c |