Talk:Function prototype

From Wikipedia, the free encyclopedia

[edit] Rewrite/Check example

I just rewrote this page, and expanded it by a decent amount. I was considering removing the stub status, but I think that this page is in a grey area right now. Plus, I'm new here and I thought it better to leave such judgements to the more experienced Wikipedians.

One thing that should definitely be checked is the accuracy of the example in the "informing the compiler" section. The only C compiler I've used is gcc, so I don't know if other compilers behave in the same way. --seliopou 02:08, 4 Apr 2005 (UTC)

I don't think the example is really good. The compiler will always catch an error wherever the function is declared, with or without a prototype. It's like this:
int fac(int n) {             /* Called function  */
    if (n == 0) {
        return 1;
    }
    else {
        return n * fac(n - 1);
    }
}
int main() {                 /* Calling function */
    printf("%d\n", fac());   /* ERROR: fac is missing an argument! */
    return 0;
}
This will result in two errors - called from main, no matching function fac(void), candidate is fac(int); called from fac, fac(int) not declared.
While the other way round, an error will still be catched
int main() {                 /* Calling function */
    printf("%d\n", fac());   /* ERROR: fac is missing an argument! */
    return 0;
}
int fac(int n) {             /* Called function  */
    if (n == 0) {
        return 1;
    }
    else {
        return n * fac(n - 1);
    }
}
The compiler error message will say: from main, fac() not declared. So the whole thing is not a good example at all. It could be better if an argument is given to fac, making it say fac(10). --Deryck C. 02:32, 12 March 2007 (UTC)