Is There a Use for Function Declarations Inside Functions

Can we declare functions inside functions?

Yes, you can declare, but you cannot define. Also, you can declare function as many times you want, but define only once.

Is there a use for function declarations inside functions?

This is really a C question, because this behaviour was inherited directly from C (although it gets much more press in C++ because of the most vexing parse).

I suspect the answer (in the context of C, at least) is that this allows you to scope the existence of your function declarations to precisely where they're needed. Maybe that was useful in the early days of C. I doubt anyone does that any more, but for the sake of backward compatibility it can't be removed from the language.

C++: Why does function declaration is allowed inside another function but not function definition?

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.

Function declaration inside of function — why?

It's definitely not idiomatic C, despite being fully valid (multiple declarations are okay, multiple definitions are not). It's unnecessary, so the code will still work perfectly without it.

If at all, perhaps the author meant to do

void test (int *int_pointer);

int main (void) {

...

}

in case the function definition was put after main ().

C++ - Function declarations inside function scopes?

Although I had no idea you can do this, I tested it and it works. I guess you may use it to forward-declare functions defined later, like below:

#include <iostream>

void f()
{
void g(); // forward declaration
g();
}

void g()
{
std::cout << "Hurray!" << std::endl;
}

int main()
{
f();
}

If you remove the forward declaration, the program won't compile. So in this way you can have some kind of scope-based forward declaration visibility.

Can we have functions inside functions in C++?

Modern C++ - Yes with lambdas!

In current versions of c++ (C++11, C++14, and C++17), you can have functions inside functions in the form of a lambda:

int main() {
// This declares a lambda, which can be called just like a function
auto print_message = [](std::string message)
{
std::cout << message << "\n";
};

// Prints "Hello!" 10 times
for(int i = 0; i < 10; i++) {
print_message("Hello!");
}
}

Lambdas can also modify local variables through **capture-by-reference*. With capture-by-reference, the lambda has access to all local variables declared in the lambda's scope. It can modify and change them normally.

int main() {
int i = 0;
// Captures i by reference; increments it by one
auto addOne = [&] () {
i++;
};

while(i < 10) {
addOne(); //Add 1 to i
std::cout << i << "\n";
}
}

C++98 and C++03 - Not directly, but yes with static functions inside local classes

C++ doesn't support that directly.

That said, you can have local classes, and they can have functions (non-static or static), so you can get this to some extend, albeit it's a bit of a kludge:

int main() // it's int, dammit!
{
struct X { // struct's as good as class
static void a()
{
}
};

X::a();

return 0;
}

However, I'd question the praxis. Everyone knows (well, now that you do, anyway :)) C++ doesn't support local functions, so they are used to not having them. They are not used, however, to that kludge. I would spend quite a while on this code to make sure it's really only there to allow local functions. Not good.

About function declarations in functions

This can also be used to limit the visibility of a function declaration to only one specific function. For example:

file1.cpp:

#include <iostream>
void f(double d) {
std::cout << d << '\n';
}

file2.cpp:

int main() {
void f(double);
f(1); // prints 1
}

void g() {
f(2); // error: 'f' was not declared in this scope
}

Nested function in C

You cannot define a function within another function in standard C.

You can declare a function inside of a function, but it's not a nested function.

gcc has a language extension that allows nested functions. They are nonstandard, and as such are entirely compiler-dependent.

Why allow function declarations inside function bodies

Whilst many C++ applications are written solely in C++ code, there are also a lot of code that is some mixture of C and C++ code. This mixture is definitely something of C++'s important part of its usefulness (including the "easy" interfacing to existing API's, anything from OpenGL or cURL to custom hardware drivers that are written in C, can pretty much be used directly with very little effort, where trying to interface your custom hardware C driver into a Basic interpreter is pretty diffcult)

If we start breaking that compatibility by removing things "for no particular value", then C++ is no longer as useful. Aside from giving better error messages in a condition that is confusing, which of course is useful in itself, it's hard to see how it's useful to REMOVE this - and that's of course assuming NONE of the C++ is using this in itself - and I wouldn't be surprised if it DOES happen at times even in modern code (for whatever good or bad reasons).

In general, C++ tries very hard to not break backwards compatibility - and this, in my mind, is a good thing. That's why the keyword static is used for a bunch of different things, rather than adding a new keyword, and auto means something different now than it used to in C, but it's not a "new" keyword that could break existing code that happened to use whatever other word chosen (and that is a small break, but nobody really used it for the past 20 years anyway).



Related Topics



Leave a reply



Submit