Stack and Return question

Hi all.
I am using Arduino IDE and ESP32 module.
I need some help in understanding this:
Please bear in mind I am just more than a beginner in C++.

Say I have the following functions

void function_one()
{
//Do something
function_two();
}

void function_two()
{
//Do something
function_three();
}

void function_three()
{
//Do something
function_one();
}


Is the above correct.
Does this cause issues with the stack, because function_three is called directly?
Regards

A recursive program, with no way out will eventually cause stack issues.

each function call consumes memory on the stack and is normally not an issue.

but the functions you describe are cyclical and there needs to be something that prevents them from running endlessly

Hi,thank you for the reply.
So, could you please tell me how do I call function_one safely.
Regards

what are you trying to do? why do you think you need this cyclical function structure?

What are you actually trying to accomplish? We could play 100000 questions or you just get right to the point so we can get to the point.

Hi,
I am in the early stages of a project that used a TFT display with some menus.
So, function_one would present the menu, with choices.

The user would select, via click, function_two.
function_two would select function_three.
Finally function_three must call function_one to show the starting screen.
Unfortunately I do not have any code yet, as I am structuring the program on paper.

Any guidance will be greatly appreciated.

Regards

Why use recursion? If 1 calls 2 and 2 calls 3 and 3 is supposed to go back to 1. Instead, drop out of 3, returning to 2 and then drop out of 2 to end up back at 1.

https://docs.arduino.cc/learn/programming/functions

Hi
thanks again.
I think I understand now.
So function_one must always be reached by a return , otherwise the program will crash.
Is that correct?
Regards

why?

in a menu system i implemented, i used an array to keep track of the menu item at each level with an index for each level.

presumably there a print for each menu item at each level, so returning back to a previous menu item simply prints that menu item

no need for recursion

Hi again.
Thank you to all.
Thank you for helping.

Regards

void function_MainMenu()
{
// Display the main menu and on selection of a sub-menu, call its funciton
function_MenuLevelTwo();
}

void function_MenuLevelTwo()
{
// Display the second-level menu and on the selection 
// of an action, call its function
function_ActionLevelThree();
return; // Go back to function_MainMenu
}

void function_ActionLevelThree()
{
// Perform the action
return; // Go back to function_MenuLevelTwo which goes back to MainMenu
}

Would SwitchCase work well here?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.