Coding practice question, calling function within a function

I looked up some stuff before writing this, about how you can't use a nested function in a function, and that you would need to have a nested struct/class. That isn't what I seem to be doing, and what I am doing compiles just fine.

But what I am wondering is: If it's bad practice to call (I hope that's the right terminology) another function from within a function that isn't your main function.

Something like what is seen in my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

void Player1Turn();
void GetApples();

int main()
{
    cout << "Initial message!\n";
    Player1Turn();
}

void Player1Turn()
{
    cout << "It is now Player 1's turn.\n";
    GetApples();
}

void GetApples()
{
    cout << "Player 1 obtained some apples!\n";
}
Initial message!
It is now Player 1's turn.
Player 1 obtained some apples!


What I'm talking about is how I first have the program call Player1Turn() in my main function, but then Player1Turn() then calls GetApples().

Is this okay to do, or is it bad practice and should I try a different way of doing things?

(This is only an example code, the actually command console game I'm making is more complicated, so I wanted to keep my post short and to the point.)


P.S. Does anyone know how long it takes for a topic to be archived? I can't find anything that says it. A week? 30 days? 60 days?
Last edited on
It's perfectly fine.
Okay, good to know, thanks. I guess my mentality when posting this was that I thought what I had looked like a nested function, and if C++ doesn't allow that, then it must be bad practice, or something.
Last edited on
Trying to define a function inside another function is illegal.

But having one function call another function is fine. In fact, it's pretty much essential for structuring your code in a sensible way.
> I guess my mentality when posting this was that I thought what I had looked like a nested function,
> and if C++ doesn't allow that, then it must be bad practice

See: http://www.stroustrup.com/C++11FAQ.html#lambda
Topic archived. No new replies allowed.