I looked at the below question to check if functions can be defined within functions.
Can we have functions inside functions?
The accepted answer says it as NO. I tried it and got the same result.
But when I tried compiling the below code (only declaration), it does compile. I'm not really sure why it is allowed.
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int a, *b, f(int c); //trying out multiple name declaration
int f(int c);
}
C++: Why does function declaration is allowed inside another function but not function definition?
Because standard says so (or doesn't disallow it explicitly). Side note: Same applies to declaration of global variables within block scope.
Why didn't the standard committee disallow it, you might ask. Not all of the rationale for every rule, and especially lack of hypothetical rules, of the standard are documented, but I may be able to conjecture in this case.
What must be understood that C++ was originally built on the C language, and compatibility with C was a high priority at the time of standardisation (and I believe it still is). So, I feel fairly confident to say that function (and global variable) declarations are allowed within block scope in C++, because they are allowed in C.
Why are function declarations allowed in C, you might ask as well. As far as I know, the use of block scope function declarations has declined in modern C, and it's a relic from pre-standardisation days. We can probably continue the language heritage to B language. I don't know much about B myself, but the example code in wikipedia happens to have a block scope function declaration.