Question about functions
Apr 1, 2017 at 4:36am UTC
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
Apr 1, 2017 at 5:05am UTC
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();
}
Apr 1, 2017 at 5:09am UTC
Apr 1, 2017 at 1:57pm UTC
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();
}
Apr 2, 2017 at 1:43am UTC
@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.