Martin
Martin

Reputation: 711

C++ functions problem

Is there a way for functions to call each other i.e.

void menu()
{
some code here
play();
...
}

int play()
{
...
menu();
...
return 0;
}

Upvotes: 0

Views: 90

Answers (2)

Khaled Alshaya
Khaled Alshaya

Reputation: 96939

Add the declaration of the second function at the top of your code file:

int play();

void menu()
{
   // some code here
   play();
   // ...
}

int play()
{
   // ...
   menu();
   // ...
   return 0;
}

This is called a forward declaration, and it informs the compiler that an identifier will be declared later.
It is a way of denoting a function so that you can call it before you provide the complete definition.

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799560

Yes, but this is almost never what you want to do since careless use will break the stack.

Upvotes: 1

Related Questions