Graeme Graeme - 3 months ago 8
C Question

What is the purpose of this line? (Function declaration)

I'm working through K & R to learn programming. Going well so far, but I'm unclear about the role of a line of code from section 1.8 (functions).

In section 1.8, the authors show you how to create a function to raise one integer to the power of another integer.

I've pasted the code below, as it was written in the book. Everything outputs fine. But I don't know why they've included this line at the top:

int power(int m, int n);


The book doesn't mention it, apart from saying that the program will raise integer m to the power n. If I remove the line from the code, the program still outputs as it should.

If I understand this correctly, the line

int power(int base, int n)


creates the function, and the braces underneath define the function. Then the braces under main call the function to output the chart.

So all that seems to make sense. But I don't see what the very top line does.

It could be extraneous, but it seems far more likely that I'm missing something. Can anyone enlighten me as to why that line is there?

#include <stdio.h>

int power(int m, int n);

/* test power function */
main()
{
int i;

for (i = 0; i < 10; ++i)
printf("%d %d %d\n", i, power(2,i), power(-3, i));
return 0;
}

/* power: raise base to n-th power; n >= 0 */

int power(int base, int n)
{
int i, p;

p = 1;
for (i = 1; i <= n; ++i)
p = p * base;
return p;
}

Answer

The first line is the declaration of the function. The block of code at the bottom is the definition of the function.

Starting with the 1999 version of the ISO C standard, it's illegal (a constraint violation) to call a function without a visible declaration; the declaration must precede the call.

For a simple program like this one, you could just write the full definition of power() before the definition of main() (since a definition also provides a declaration), but for more complex cases (such as recursive calls) you often need to provide a separate declaration.

For larger programs, it's common to collect all the function declarations in a header file (foo.h, for example), and the corresponding definitions in a source file (foo.c, for example). A #include "foo.h" directive is used to make the declarations visible in other files. You'll see that kind of thing later in the book.

(In the 1990 and earlier versions of C, which is what K&R2 covers, there are cases where you can call a function without a visible declaration -- but it's still a very good idea to provide explicit declarations anyway.)

Incidentally, the declaration of the main program should really be int main(void) rather than just main().

Terminology: a "prototype" is a function declaration that specifies the types of the parameters.

int power(int base, int n);    /* a declaration that's also a prototype */
int power(int, int);           /* likewise */
int power();                   /* a declaration but not a prototype */

(Parameter names are required in a definition, but optional in a standalone declaration.)

As a special case, a prototype for a function with no parameters uses (void), since empty parentheses already mean a non-prototype declaration. So int main(void) is a prototype, but int main() isn't.

Non-prototype declarations are "obsolescent", meaning that they could in theory be removed from a future language standard. But they've been obsolescent since 1989, and even in the new 2011 ISO C standard the committee hasn't seen fit to remove them.