[extern] return-type FuncName ( param1_type [var1], param2_type [var2], .... ) ; |
Note:
|
Example of function declarations:
extern float f( float x ) ; float f( float x ) ; // extern can be omitted float f( float ) ; // the parameter name can be omitted |
(Personally, I prefer to write float f(float x), it looks like the function definition (so I can cut and paste to create the function declaration easily)
|
|
|
|
|
Grpahically:
#include <stdio.h> int main(int argc, char *argv[]) { short x = 2; // *** short !!! int y = 0; y = f(x); // Assumes: int f(int x) printf("x = %d, y = %d\n", x, y); } /* --------------------------------------------------- Function f( ) is defined AFTER main's use of f( ) --------------------------------------------------- */ int f(short x) { return(x*x); } |
We can declare the function f( ) at the start of the program to solve this problem:
#include <stdio.h> int f(short x); // function declaration !!! int main(int argc, char *argv[]) { short x = 2; // *** short !!! int y = 0; y = f(x); // Assumes: int f(int x) printf("x = %d, y = %d\n", x, y); } /* --------------------------------------------------- Function f( ) is defined AFTER main's use of f( ) --------------------------------------------------- */ int f(short x) { return(x*x); } |
How to run the program:
|
|