Hey everyone
I am trying to declare a recursive function within a function to use it to my purposes
For instance
/code
#include <iostream>
using namespace std;
Void insert(int a)
{
void delete(int b)
{
if (b==0)
return
delete(--b);
};
delete(a);
} /code
I tried doing this but an error message says function declaration is not allowed here
How can I make this move possible?
Thanks
C++ doesn't let you nest functions (well, there are lambda functions, but since this is the beginners forum, let's ignore those for now.
You could declare the nested function as static. That would make it invisible to anything outside the current compilation unit (.cpp file):
"delete" is a reserved keyword in C++ so you can't use it as a function name. Here is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
what I'm talking about with the name changed to "destroy"
#include <iostream>
using namespace std;
static void destroy(int b)
{
if (b==0)
return;
destroy(--b);
};
void insert(int a)
{
destroy(a);
}
@dhayden
Thank you so much for replying but I see that you separated both functions from each other. I wanted to make a recursive function within a function. Or did you do so for explanation?
Thank you so much again
you cannot put a normal function inside another in this language. Most languages prevent this, actually.
you can call them inside each other, and even rig it so that one can call the other that calls the first again. You can surely accomplish whatever it is you want to do, but not the way you want to do it...