The Operator "return" - page 2

 

Thank you guys for your help so far. Okay I guess I really don't need to "skip" a function like RaptorUK said.

I somewhat understand what Ickyrus' explanation. But when the function is terminated, where does it resume? Does it start from the very beginning again or what?

 
resumes from the begining when its called removed destroyed when it meets a retun command -its definition remains in the written code. if you have written instruction to follow they only get executed when you actually do the instructions otherwise they just sit there being potentially usful to you in a book or on a sheet of paper. For example baking bread you have the instruction to bake bread but you can't make bread until you have the ingreadients (in this analagy ingretients are variables) and when you have finished making bread you thow the rubbish away and eat the bread. Now you cant follow the instructions until you have the ingrediants again.
 
Thanks for the explanation, but I'm still a bit confused. Using the print functions I used in my example, can you explain the logic behind the code, why it returns to the beginning of the entire code when it executes return; and never gets to the end, even though it's a function.
 
jackyan22:
Thanks for the explanation, but I'm still a bit confused. Using the print functions I used in my example, can you explain the logic behind the code, why it returns to the beginning of the entire code when it executes return; and never gets to the end, even though it's a function.

Execution returns to the code immediately after the code that called the function.

In this example the sequence would be as follows . . .

1. function_a is called and if it returns true . . .

2. . . . function_b is called

3. regardless of function_b being called or not, start() ends and returns 0

start()
   {
   
1.   if( function_a() )
2.      function_b();

3.   return(0);
   }

bool function_a
   {
   return(true);
   }

void function_b()
   {
   return;
   }
 

Ok I will try again, BTW when the Bread function was executed that is you start following the instructions the function returns Bread.

int start() //<< new price data from broker lets do something with it 
  {<< many functions go between these 2 brackets as well
//----
   Print("1"); 
     {
      Print("2");
        {
         Print("3");
         Print("4");
          {  // << many functions go between these 2 brackets
          
           return; //<< sends you back to the controling chart you put the ea on 
                   //   i.e the one with the smiley face default return value is 0
           
          }  // << and end here
          
          // execution Never reaches this point as it was told to return before getting to this line
         if(Print("4")==false  //Never executed
          {
           Print("5");  //Never executed
          }
       }  
       }
    Print("end of EA"); //Never executed
      
     
      
//----
   return(0); //Never executed
  } //<< and end here as well, Braces must balance even though execution never reaches here.
 

Thank you guys again. Your examples are very clear. But, in RaptorUK's example, it returns to int start (), since it called the operator "return," in Ickyrus' example, the use of return also sends it back to int start(), but it is Print ("4") that called it? What accounts for the discrepancy?

 

Print("4") does not do any function it is a function in its own right its purpose is to send text characters to the journal tab.

return(0) is a command to fill a reserved memory location with a number and stop doing any further instruction that follow Doing the return command, that is instruction inside the functions definition. If 'shop closed' do the command 'turn arround'

 
Tell robot to go shopping.

robot starts shopping program 

int start()
{
string ShoppinglList ;

ShoppinglList = GetShoppingList() ;
WhichShop = GetDirectiontoShop() ;

if (WhichShop == "Dont know that one") return ;

walktoshop ;
if (shopclosed) return
GetItemsOnList(ShoppinglList) l
walktohome ;
return ;
) // end of start()

string GetShoppingList()
{
string List ;

if( Flour == Low ) List=List + Flour;
if( Milk == Low  ) List = List+Milk ;
if( Apples == Low) List = List + Apples ;
return(List) ;
}

string GetDirectiontoShop()
{
if( Response() == WalMart ) return("WalMart") ;
if( Response() == CornerShop  ) return("CornerShop") ;
if( Response() == Asda)  return("Asda") ;
return("Dont know that one") ;
}


The robot end up standing outside the shop waiting for the command walktohome if the shop is closed
also once the return command is executed inside the start() function the shopping list is forgoten.
 
jackyan22:

Thank you guys again. Your examples are very clear. But, in RaptorUK's example, it returns to int start (), since it called the operator "return," in Ickyrus' example, the use of return also sends it back to int start(), but it is Print ("4") that called it? What accounts for the discrepancy?

Just in case it isn't clear . . . .

Print("4"); // << many functions go between these 2 brackets
          {
           return;
          }

. . . no functions can be between the braces, the functions are declared outside of start(), what can go inside those braces are calls to the functions declared outside of start()

In the function start() when a return is encountered start() ends and the EA sits doing nothing just waiting for the next tick, when the next tick arrives start() is called again.

 

I've always understood the use of return in custom functions. Like your GetshoppingList() and GetDirectiontoShop(). But I still don't understand where will the EA return to if return is executed. How do I know what classifies a function as "worthy" of being returned too (in this case it's int Start())? I mean in the line: if(shopclosed), isn't that a function? Since you are checking true/false statements.


edit: see next page!

Reason: