float f(float a)
{
return a*a;
}
int main()
{
int x, y;
x = 4;
y = f(x);
}
|
|
Example:
The int value 4 is represented as:
00000000 00000000 00000000 00000100
|
x = 4; // x = 00000000 00000000 00000000 00000100
// because x is an int, use 2's complement encoding !
y = f(x); // 1. convert the 2's complement encoding:
//
// 00000000 00000000 00000000 00000100
//
// into IEEE 754 encoding for the same value:
//
// 01000000 10000000 00000000 00000000
//
// 2. pass the converted value as parameter
// to the function f
//
// 3. Execute f
//
// 4. Receive the return value from f
//
// *** The return value is in the IEEE 754 encoding !!!
//
// 5. Before storing the return value in y,
// we must:
//
// convert the IEEE 743 encoding into
// 2's complement encoding !!!
|
|
|
|
|
|
|
void print ( double x )
{
printf("Number = %lf\n", x );
}
double square( double x )
{
double r; // Define a local variable
r = x * x; // Statement
return ( r ); // Return statement
}
int main( int argc, char *argv[] )
{
double a, b;
a = 4.0;
b = square( a ); // <---- First use
// square() was defined above
print( a ); // <----- First use
// print() has been defined above
print( b ); // Call function print
}
|
Observation:
|
Therefore, the C compiler knows the data types of the parameters (and return value) and:
|
int main( int argc, char *argv[] )
{
double a, b;
a = 4.0;
b = square( a ); // <---- First use
// square() has not been defined yet !!!
print( a ); // <----- First use
// print() has not been defined yet !!!
print( b ); // Call function print
}
void print ( double x )
{
printf("Number = %lf\n", x );
}
double square( double x )
{
double r; // Define a local variable
r = x * x; // Statement
return ( r ); // Return statement
}
|
Observation:
|
Therefore, the C compiler does not know the data types of the parameters (and return value) and:
|
|
How to run the program:
|
|
Example:
double square( double x ) // Function header
{ // Function body
double r;
r = x * x;
return ( r );
}
|
|
Example:
extern double square( double x ) ; // Function declaration or: double square( double x ) ; // Function declaration |
|
|
void print ( double x ); // function declaration
double square( double x ); // function declaration
int main( int argc, char *argv[] )
{
double a, b;
a = 4.0;
b = square( a ); // <---- First use
// square() has been declared above !!!
print( a ); // <----- First use
// print() has been declared above !!!
print( b );
}
void print ( double x )
{
printf("Number = %lf\n", x );
}
double square( double x )
{
double r; // Define a local variable
r = x * x; // Statement
return ( r ); // Return statement
}
|
How to run the program:
|
|
|
(The C compiler will have no way to knowing that the wrong type assumption had been made)
We will discuss this topic next.....