Working with nested functions

Hi,
After a bit of researching and much experimenting still haven't been able to figure out how to "return" to each Callout Function that involves nested functions... multilevel menu system. Here's a picture and some code to help explain. Hope it makes sense

Observation:

  • No issues 'return'ing to the first Callout to the MainMenu, except when calling two or more functions... then it seems like the 'return' just goes to the most recent callout.

Is there a coding technique for managing through these functions?
Or am I approaching this all wrong?

Thanks,

MenuFunctions

void loop() {
  // do things
  MainMenu();
  // do things
}

void MainMenu() {
  do 
    {// Serial Terminal input choices 1,2,3
    switch (input){
      case 1:
        SubMenu_1();
        break;
      case 2:
        SubMenu_2();
        break;
      case 3:     
        return;   //RETURN ONLY to original calling function 
    }
  } while (input != 3);
  return;         //RETURN ONLY to original calling function 
}
      void SubMenu_1() {
        // Terminal Serial input choices:  1,2,3)
        switch (inputchoice){
          case 1:
            SubMenu_1a();
            break;
          case 2:
            SubMenu_1b();
            break;
          case 3: // EXIT to MainMenu
            //use "break;","return;" or "MainMenu();"  ??
        }
        //use "return;", "MainMenu();" or NOTHING  ??
      }
            void SubMenu_1a() {
              // do things then 
              // Terminal Serial input choices:  1,2,3)
              switch (inputchoice){
                case 1:
                  //do things
                  break;
                case 2:
                  //do things
                  break;
                case 3: // EXIT to MainMenu
                  //use "break;","return;" or "MainMenu();"  ??
              }
              //use "return;", "MainMenu();" or NOTHING  ??
            }
void SubMenu_2() {
  //conitnues similar to Submen_1...
}

Just put a 'return;' after each callout. When that callout returns the caller will then return all the way back up to Main.

      void SubMenu_1() {
        // Terminal Serial input choices:  1,2,3)
        switch (inputchoice){
          case 1:
            SubMenu_1a();
            return;
            break;
          case 2:
            SubMenu_1b();
            return;
            break;
          case 3: // EXIT to MainMenu

Tried that but didn't work. Return will kill the entire nested function process and go back to the void loop()... from what I have read and tested.

The Subx and Proc functions all need to go back directly to MainMenu. If I use 'return' in each of those, then it kicks out of the entire nested function... going directly back to void loop().

Want to have ONLY the MainMenu() control returning to the Functionx() that called it. While also being able to having all the Subx/Proc functions going only back to the MainMenu().

Don't return from MainMenu until you are done with the main menu.

Correct. I only use "return;" in the MainMenu()...

How should I properly jump among the subfunctions under the MainMenu without causing those to be the location the MainMenu() will return?

I'm calling "MainMenu();" from each of the Subx/Proc F's to get back. Is there a better technique?

Plus I want to go the 'Functionx()" from the MainMenu() and NOT the void Loop().

That is not good because every call to MainMenu that never returns with a 'return' is adding data to the stack. This will eventually fill all available memory and cause a crash. Each function, when it is done, should return to the caller.

I'm just starting to understand that issue as well, but thought the 'return'... when eventually used in MainMenu() would CLEAR the Stack issues? or is that not correct?

Don't do that... you are not "returning" in that case... eventually your program will run out of memory and die.

If you go Main -> SubMenu1 -> Proc1... then you must go back the same way.

Proc1 -> SubMenu1 -> Main.

You can use "return" to exit each level, and go back to the one prior.

I guess I could rollup all of the Menu stuff into a single VERY long Function to call? That would eliminate all of the jumping around between functions. Was trying to break it out for easier coding/understanding.

OK... THAT makes sense. It's like alleyways. I was jumping outside of that thinking. Thank you!

i implemented a menu system where the code returns to the same menu item in the next higher level, from which that sub-menu was called using an array that keeps track of the menu item at each level and an idx that track the level

I would suggest reading up on finite state machines (FSM). Any other approach will end up getting unbearably complex for any but very trivial menu systems. A well-designed FSM can expand to an almost unlimited extent while remaining understandable and easily maintainable. I've done menu systems with dozens of menus, and hundreds of functions, even systems wth multiple cooperating FSMs for handling user input, motion control, messaging and other functions. Even years later, they are still easy to expand and maintain.

maybe a dumb question... does the nature conclusion of a called function result in the memory being resolved as if 'return' was used?

yes

Thank you.

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