0

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);
}
pasha
  • 2,035
  • 20
  • 34
  • Bottom line - it's allowed because the C++ Standard says it is. Also, in modern C++, you can define functions inside other functions, in the form of lambdas. –  Aug 13 '18 at 13:46
  • @NeilButterworth, Yes, I agree, but there must be some kind of application for it. I just want to know that. – pasha Aug 13 '18 at 13:48
  • @pasha No, there is not necessarily any use for a given quirk of the grammar. It might just be a harmless nonsense. – Caleth Aug 13 '18 at 13:53
  • 2
    it's a forward declaration. – dan Aug 13 '18 at 13:57
  • @dan forward declaration of **what**? I never heard people saying *forward declaration* with regards to functions. – SergeyA Aug 13 '18 at 14:15
  • @SergeyA forward declaration is simply a declaration that is not a definition. This is a (forward) declartion of a function: `int f(int c);` – eerorika Aug 13 '18 at 14:17
  • I am not sure if there is any reason for that. I see no way of actually using this feature for any reason. – SergeyA Aug 13 '18 at 14:17
  • @user2079303, no, this is a declaration of function. Not **forward** declaration of it, at least, this is a terminology used in Standard. To the best of my knowledge, the term *forward declaration* is only used for incomplete types. – SergeyA Aug 13 '18 at 14:18
  • @SergeyA oh, really? How about this quote from the standard `template<> auto g(double); // OK, forward declaration with unknown return type`. Sure, that is a forward declaration of a function template, but does that matter? As far as I know, the standard doesn't actually define what a **forward** declaration is and it's simply an adjective describing the declaration rather than a technical term. Am I mistaken? – eerorika Aug 13 '18 at 14:21
  • @user2079303 I think, you might be right. Looks like standard doesn't use *forward declaration* as a normative term. It looks like a laymen speak. But still, I would argue that in laymen speak, a declaration of a function is usually simply called a declaration, while a declaration of a class is called forward declaration. Supporting evidence: https://www.google.com/search?q=forward+declaration+cppreference (it goes to show that cpp reference mentions this term when it talks about structs) - and when it talks about functions, fwd declaration only comes from standard example. – SergeyA Aug 13 '18 at 14:30
  • Different isn’t the same. – Pete Becker Aug 13 '18 at 20:27
  • FWIW, you can change and add default arguments to function when redeclaring them, and the new default values are only valid in the scope of the new declaration. I doubt this is the reason for this "feature" but the more you know... ;) – Holt Aug 14 '18 at 08:31

1 Answers1

2

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • My view of the standard is like, "If it is allowed, there is a for reason it." So, I wanted to know it. If you say random things of past exist in the standard, that's new for me. – pasha Aug 14 '18 at 08:55
  • Backwards compatibility is a reason. – eerorika Aug 14 '18 at 08:57
  • Makes sense. But somebody must've said something like, "Boy, why are we supporting the B language stuff for c++17". – pasha Aug 14 '18 at 09:11
  • @pasha I don't think people go through language rules thinking how old history they have, and decide that something should be done if they're old. C++ still has *a lot* in common with B. – eerorika Aug 14 '18 at 17:53