View Single Post
Old Sep 2nd, 2005, 8:02 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5 grumpy is on a distinguished road
If the function is defined (i.e. it's body is implemented) in another source file, then the caller needs to see a function declaration (aka a prototype) before it can call it. The compiler must encounter the declaration before (i.e. above) the attempt to call the function. This is strictly enforced in C++ but is optional in C [at least in the 1989 C standard], but is usually considered good style in C.

If the function definition and the caller are in the same source file, things get a bit more problematical. If the function definition is above the call point, than that function definition implicitly provides a prototype. And the compiler will complain bitterly (about a function that is already defined) if a function prototype occurs after the definition, viz.
int someFunc(double x, int y)
{
    return something(x, y);
}

int someFunc(double, int);   // error:  someFunc is already defined

int main(int nNumberofArgs, char* pszArgs[])
{

    someFunc(1,2);

}
If the function definition is in the same source file as the caller, but AFTER the call point, then the prototype must be visible first, viz.
int someFunc(double, int);   // required so we call it

int main(int nNumberofArgs, char* pszArgs[])
{

    someFunc(1,2);

}

int someFunc(double x, int y)
{
    return something(x, y);
}
grumpy is offline   Reply With Quote