Reputation: 1113
#include <stdio.h>
int main()
{
void foo();
printf("1 ");
foo();
}
void foo()
{
printf("2 ");
}
Output:
1 2
How declaring functions inside functions work? Does it mean that foo() function can only be called by main()?
Upvotes: 8
Views: 6584
Reputation: 11
We can declare a function inside a function, but it's not a nested function. Because nested function definitions can not access local variables of the surrounding blocks, they can access only the containing module's global variables. So it is better to declare any function above the main function so that you can call them anywhere in any function.
Upvotes: 0
Reputation: 9841
Yes, you can declare, but you cannot define. Also, you can declare function as many times you want, but define only once.
Upvotes: 14