|
Function f( ) is defined before it was used:
#include <stdio.h> float f(float a, float b) // Compiler knows params are floats { return(a*a + b*b); } int main(int argc, char *argv[]) { int x = 3, y = 4; int z; z = f(x,y); // Compiler will convert int to float printf("%d^2 + %d^2 = %d\n", x, y, z); } |
DEMO: /home/cs255001/demo/tmp/demo.c
Function f( ) is not defined or declared before it was used:
#include <stdio.h> int main(int argc, char *argv[]) { int x = 3, y = 4; int z; z = f(x,y); // Compiler will pass x,y as int printf("%d^2 + %d^2 = %d\n", x, y, z); } float f(float a, float b) // Function needs float - error ! { return(a*a + b*b); } |
In this case, the compiler can detect the error and reports an error
|
Function f( ) is now declared before it was used:
#include <stdio.h> float f(float a, float b); // Function declaration int main(int argc, char *argv[]) { int x = 3, y = 4; int z; z = f(x,y); // Using function f( ) printf("%d^2 + %d^2 = %d\n", x, y, z); } float f(float a, float b) // Function definition { return(a*a + b*b); } |
DEMO: /home/cs255001/demo/tmp/demo.c
Function declarations are needed when the functions are defined inside a different file:
progFile1.c | progFile2.c |
---|---|
#include <stdio.h>
int main(int argc, char *argv[])
{
int x = 3, y = 4;
int z;
z = f(x,y); // Using function f( )
printf("%d^2+%d^2=%d\n", x, y, z);
}
|
// Function f( ) is defined
// in this file
float f(float a, float b)
{
return(a*a + b*b);
}
|
In this case, the C compiler cannot detect the error !!
You see the error whan you
run the program, it
outputs
3^2 + 4^2 = 0 !!??
(Compiler had
generated
incorrect assembler instructions !)
Function declarations are needed when the functions are defined inside a different file:
progFile1.c | progFile2.c |
---|---|
#include <stdio.h> float f(float a, float b); int main(int argc, char *argv[]) { int x = 3, y = 4; int z; z = f(x,y); // Using function f( ) printf("%d^2+%d^2=%d\n", x, y, z); } |
// Function f( ) is defined
// in this file
float f(float a, float b)
{
return(a*a + b*b);
}
|
The function declaration will fix the error
DEMO: /home/cs255001/demo/tmp/demo.c + demo2.c
You can have multiple (non-conflicting) declarations of the same function:
progFile1.c | progFile2.c |
---|---|
#include <stdio.h> float f(float a, float b); float f(float a, float b); int main(int argc, char *argv[]) { int x = 3, y = 4; int z; z = f(x,y); // Using function f( ) printf("%d^2+%d^2=%d\n", x, y, z); } |
// Function f( ) is defined
// in this file
float f(float a, float b)
{
return(a*a + b*b);
}
|
You will get compile errors if the declarations are conflicting (not the same).
You can put function declarations inside a function:
progFile1.c | progFile2.c |
---|---|
#include <stdio.h> int main(int argc, char *argv[]) { int x = 3, y = 4; int z; float f(float a, float b); z = f(x,y); // Using function f( ) printf("%d^2+%d^2=%d\n", x, y, z); } |
// Function f( ) is defined
// in this file
float f(float a, float b)
{
return(a*a + b*b);
}
|
In this case, the function declarations are subjected to the scoping rules for local variables !