0

I'm fairly new to this, and I haven't been able to find any answers after searching repeatedly.

I have a Startscreen() function that I run during my setup sequence, and within that function i use various while loops and if/else conditionals. This function runs the startup screen of my program on my touch screen.

Well, when certain conditions are met inside a switch case, inside a while loop, I want to load my main screen and then immediately stop the whole Startscreen() function, like this:

LoadMainScreen();
return;

However, I end up seeing my main screen load up, and then my Startscreen() function continues!

Apparently, "return;" does not work as advertised. If I wanted to break out of the current loop, I'd use "break;", right?

So my question is, does it matter that I'm using return; inside a while loop, inside a switch case? How do I exit the whole top-level function completely?

I already tried breaking out of the function and going straight to my main loop this way:

LoadMeasScrn();
loop();
return;

and the same thing happens.

So here's (most of) my entire code:

//////////REMOVED/////////THANKS FOR YOUR ANSWERS/////////////
  • It's impossible to say from those tiny snippets. We need to at least see the structure of your whole code. – Majenko Nov 22 '16 at 20:11
  • Well, I can't post my code. It's 800 lines long, anyway, so I doubt anyone would want to sift through it. Not sure what I could do to better explain the situation. This is more of a question of how the return function is supposed to work. – LegitimateWorkUser Nov 22 '16 at 20:14
  • At the very least show us how and where those functions are being called. – Majenko Nov 22 '16 at 20:15
  • Return does what it says it does. The problem isn't with the return - it's either with how you are using it, or the rest of the structure of your code. – Majenko Nov 22 '16 at 20:23
  • Not here! In your question! – Majenko Nov 22 '16 at 20:23
1

Your second outermost if() clause in StartScreen(), the one that begins with if (frametype == 0 || frametype == -1), contains an infinite while loop. That loop contains no break statements, and calls StartScreen() -- it recurses into itself!

Without my trying to follow the program logic, I can at least posit that the code is in a second or higher recursion when it calls return, thus returning to where it had called itself. So, yes, if that is correct, it will still be executing within StartScreen() after the return statement. The return statement wouldn't have "failed" in that case; it would have resumed the at the point where the its prior instance called itself.

If that sounds convoluted, it's because it is! There can be good reasons to recurse, if you intended to, your know in what circumstances the code will recurse, and you know that recursiing will stop before you run out of stack space. Carefully review your program logic; this may not be one of those instances....

  • I follow you. I replaced the recursion cases with something else I found. I'm now using a label at the very beginning of startscreen() startover: I'm then calling goto startover; Does this effectively start the function over at the beginning without the recursion problem? – LegitimateWorkUser Nov 23 '16 at 1:03
  • Yes, that's quite different to recursion. Did it alter or help with the original issue? – JRobert Nov 23 '16 at 1:13
  • I think so! It had not occurred to me before that the program had gone back to a previous instance of startscreen(). Looking back though, I can see where it might have occurred without me knowing. – LegitimateWorkUser Nov 23 '16 at 1:17
0

This part is wrong, for a start:

LoadMeasScrn();
loop();
return;

That would recursively call loop (which you almost never want to do) effectively putting you into loop a second time.

return does what is advertised to do. Your rather complex pseudo code demonstrates nothing. Please post a Minimal, Complete, and Verifiable example


It's 800 lines long, anyway, so I doubt anyone would want to sift through it

I would much rather read 800 lines of code than try to guess what you are doing.


You have an incredible amount of recursion. You possibly think that if StartScreen calls itself then it goes back to the start (like a goto) but no, you are now two levels deep into StartScreen.

Plus here:

void StartScreen(void) {
...
  if (stuff)
...
                Recheckframe();

...

bool Recheckframe(void) {//For use in start screen, function rechecks frame 
...
  if (otherstuf)
...
        StartScreen();  //Start all over 
        }

It's a wonder it doesn't just crash by running out of RAM.

You say "//Start all over" but it isn't starting all over. You are now digging deeper and deeper into the call stack.

Your call stack will look something like:

StartScreen -> StartScreen -> Recheckframe -> StartScreen

You need a rewrite, frankly. A state machine would probably be very appropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.