Reputation: 110432
When should a function prototype/declaration be used, or is it good practice to always include them? For example:
int square_num(int num);
int main(void) {
return square_num(3);
}
int square_num(int num) {
return num * num;
}
vs.
int square_num(int num) {
return num * num;
}
int main(void) {
return square_num(3);
}
What would be considered best practice, or is it a case-by-case basis?
Upvotes: 3
Views: 99
Reputation: 123568
Nit: "prototype" refers to a particular style of a function declaration, not the declaration itself. A function declaration or definition that specifies the type of the parameter in the parameter list is a prototype:
int square_num(int num); // declaration using prototype syntax
int square_num(); // declaration using non-prototype syntax
int square_num(int num) // definition using prototype syntax
{
return num * num;
}
int square_num(num) // definition using non-prototype syntax
int num;
{
return num * num;
}
You should always use prototype syntax unless you are having to support an ancient K&R implementation.
What you’re asking about is whether it’s good practice to have a separate declaration for a function defined in the same source file.
This is largely a matter of taste, but I strongly recommend going with the second style and defining the function before it is used. It reduces your maintenance burden if you ever need to change the return type or parameters of the function, as you only need to make that change in one place. It may not sound like much, but over a large project it can save some heartburn.
It does mean your code reads "backwards", but the tradeoff is worth it IMO.
Upvotes: 2
Reputation: 145242
The rule is simple: a function should either be defined or declared with a proper prototype before it is used in code.
Both program styles in the question are correct and which one to use is a matter of personal taste or local conventions. I personally prefer the second and would define square
as static
.
If the function square
is used in another module, it should be declared in a header file included in both modules.
Mutually recursive functions require that at least one of them be declared before use.
Older versions of the C Standard allowed the compiler to infer the prototype of function not yet defined at the point of use as taking an unspecified number of arguments and having an int
return type. This error-prone behavior is no longer condoned by recent versions of the C Standard.
Upvotes: 1
Reputation: 224677
In this particular case it's required. Because the function is used after it is defined, it must be declared before.
Had you defined square_num
before it was called you would not need a prototype.
On a side note, old pre-standard C had the concept of an implicit function declaration if it was used before it was defined. Such a function foo
would be implicitly defined as int foo()
, i.e. a function taking an unknown number of arguments and returning int
. Some compilers still allow this, but you shouldn't depend on this behavior.
Upvotes: 2