Function within a function?

Hi.
I'm making a text based rpg and I need to be able to run a function within a function, and then be able to continue with the other terms of the original function.

Can anyone show me how to do that? Links to tutorials are much appreciated.

Here's a more specific example of what I mean in psuedocode.
1
2
3
4
5
6
7
8
9
10
11
12
void repeatingFunction(){ 

//function does whatever

}
int main(){
string input;
cout<<"whatever ";
cin>>input;
repeatingFunction();
cout<<"Something else based on what function above returns";
}


Can someone help me to figure out how I would do this?

You already know how to do this. You are calling the function repeatingFunction from within the function named main. Here is code in which repeatingFunction calls yet another function named otherFunction.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void repeatingFunction(){ 

//function does whatever

// now call some other function
  otherFunction();

// now carry on with some more in repeatingFunction

}
int main(){
string input;
cout<<"whatever ";
cin>>input;
repeatingFunction();
cout<<"Something else based on what function above returns";
}
You can call functions in the middle of functions no problem.

1
2
3
4
5
6
function1()
{
    //do some stuff
   function2();
   //do more stuff
}


Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

void func1();
void func2();

bool x = 0;

int main()
{
    func1();
    return 0;
}

void func1()
{
    cout << "Func1 running" << endl;
    func2();
    if(x == 1)
    {
        cout << "X was true!" << endl;
    }
    else
    {
        cout << "X is false!" << endl;
    }
}

void func2()
{
    x = 1;
}
So if I call that function like that, and it doesn't navigate to a different function, it will just return to the function it was called from automatically?

Because I need something that can determine the input from the user and then return something, and so far the only way I could manage that was using the same if-else statement repetitively. I don't mind using it repetitively, it just makes my code super bulky.
Yes. A function is just a way of arranging code. It will not jump out of the function you are in and out of the one it was called from too (unless you make it do that). Pseudocode:

1
2
3
4
5
6
function()
{
    //do stuff
    getuserinput(); 
    //do stuff with that input
}


All fine, so long as you make the variables involved global so they are available to all the functions.
Well damn, if that had been explained in the book I have I would never have made this thread. Ha ha. Thanks everyone for your input! :D
Shoot me a private message if you're at all interested, and I'll send you my game when its finished.
Topic archived. No new replies allowed.