Question about functions

I am relatively new to function in c++ and I was testing with them to increase my understanding of them and I was wondering if you could nest functions? Like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

void allfunc()
{

void username()
{

}

}

int main()
{


reutrn 0;
}


Sorry for the terrible example but could you do that? if so can you call different variables from different functions as long as they are in the allfunc?

Thanks,
-Garrows
I dont believe that is possible but you can call another function within a function for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void func1()
{
    cout << "inside func 1" << endl;
    func2();
}

void func2()
{
    cout << "now in func2" << endl;
}

int main()
{
    func1();
}
http://stackoverflow.com/questions/4324763/c-can-we-have-functions-inside-functions

Short answer: Use lambdas for local function definitions.
kingkush – in your example program func2() needs to be, at least, forward declared so that it can be called by func1()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

void func2();

void func1()
{
    std::cout << "inside func 1" << std::endl;
    func2();
}

void func2()
{
    std::cout << "now in func2" << std::endl;
}

int main()
{
    func1();
}
@gunner funner, yes you're right. I totally forgot about that. Or else func1 wont know what func2 is.
Topic archived. No new replies allowed.