Reputation: 8356
Most people seem to declare functions outside the code block that's using them like this:
void doSomething();
void doStuff()
{
doSomething();
}
I'm wondering if it's considered a good habit to do instead like this:
void doStuff()
{
void doSomething();
doSomething();
}
The second method has smaller scope and that's generally considered good style, but why isn't almost anyone using it? Are there some drawbacks?
Upvotes: 0
Views: 722
Reputation: 935
If doSomething() is only used in this c-file, it should be declared static.
static void doSomething();
It is a good pratice to declare all static function prototypes at the beginning of a file instead of in the middle of the code line in your example.
This way, if someone is looking through your code and sees all the static function prototyes he can get quick overview of whats happening.
Upvotes: 1
Reputation: 791849
As the function has to be defined in the enclosing namespace scope, there doesn't seem to be a great advantage in restricting the declaration to the function scope.
Conventionally, having a single declaration in a header file is easier to maintain as, if the function definition needs to change, only a single declaration needs to be adjusted not a declaration in each other function where the function is used.
Upvotes: 1