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. Here's a bit of pseudocode that will hopefully show how I have things organized:

void setup(){
Startscreen(); //this screen provides user options
}
void loop(){
do all the other main functions of the program;
}
void Startscreen(){
if this thing, show this
if that thing, show that
take readings
switch(reading){
case(number):
 while(1){
 check for button presses
 if (this){
 if (that){
 LoadMainScreen();
 return; //this is supposed to quit the startscreen function completely and allow the program to move on to the main loop
case(other number);
 do other stuff
 break;
 } //end of case stuff
Do other stuff that the startscreen program may need to do //this is what I'm seeing after calling return. should not see happening
Juraj
18.3k4 gold badges31 silver badges49 bronze badges
asked Nov 22, 2016 at 20:06
7
  • It's impossible to say from those tiny snippets. We need to at least see the structure of your whole code. Commented Nov 22, 2016 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. Commented Nov 22, 2016 at 20:14
  • At the very least show us how and where those functions are being called. Commented Nov 22, 2016 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. Commented Nov 22, 2016 at 20:23
  • Not here! In your question! Commented Nov 22, 2016 at 20:23

2 Answers 2

2

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....

answered Nov 22, 2016 at 22:24
3
  • 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? Commented Nov 23, 2016 at 1:03
  • Yes, that's quite different to recursion. Did it alter or help with the original issue? Commented Nov 23, 2016 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. Commented Nov 23, 2016 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.

answered Nov 22, 2016 at 21:14
3
  • Alright, fine. Hope I don't get fired. Commented Nov 22, 2016 at 21:40
  • See amended answer. Commented Nov 22, 2016 at 23:36
  • Incredible recursion!!!!!!!!! Commented Nov 23, 2016 at 1:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.