I am ashamed to ask - but I need to know

 

I have been trying to program some EA's for a couple of months now and I suddenly realised that I may not have the correct understanding of the "return" operator.

Can somebody just explain to me exactly what happens what happens when I put a "return(0)" somewhere in my code. It could be on a new line, as part of an "if" statement or "for" loop. Does it matter? I always assumed that the control is handed back to the first statement after the "start()" function no matter where the "return(0)" appears in the code. As long as the condition is true, of course - if there is one. Is this understanding correct?

What happens if I put a different value, e.g. -1 in stead of 0 - for example "return(-1)"? Or even "return(true)"? Or does this only happen as part of a function that has to return a certain value?

I know that many of you may be shaking your head in disgust at this ignorance at the moment but I have to be sure.

Thanks for your patience with me.

 
ernest02:

What happens if I put a different value, e.g. -1 in stead of 0 - for example "return(-1)"? Or even "return(true)"? Or does this only happen as part of a function that has to return a certain value?

I know that many of you may be shaking your head in disgust at this ignorance at the moment but I have to be sure.

Thanks for your patience with me.

In general the only stupid question is the one that goes un-asked . . . (there are exceptions to this rule, trolls, etc ;-) )

OK, I'll assume you know nothing even though I know that not the case . . . :-)

When a function is declared (the first line of a function) it is given a type . . . void, int, string, bool, double, etc . . . this means that the function has an overall resultant value . . except for type void. So when the function comes to an end, either at the last line of it's code or before that, for some particular reason, it usually has to pass back a value of it's type - this is what return(type) does. I said "usually has to" . . because if the function is called without assigning it's resultant value to another variable it can simply return; examples of this are the 3 standard functions, init() start() and deinit() they are all type int but their return values are not assigned to a variable so there is no need to use return(int);

some simple examples:

bool a=true, b=false, c;

c =  AFunction();

bool AFunction()        // function is type bool so . . .  .
   {
   return(a || b);      // returns type bool
   }

=====================================================================
double a, b, c, d;   // globally defined variables

a = 3.142;
b = 12;
c = 2;

CircleArea();               // value of function not assigned to a variable

Print("Area of the circle is . . ", d, " mm squared");

int CircleArea()
   {
   d = a * MathPow(b, c);
   return;                   // global variable d  used so there is nothing to return 
   }

 
Good, thorough answer! In the last example, I would take the view that if you are never going to return anything, using void as the return type is more appropriate than using int. This makes the code clearer and possibly slightly more efficient (depends how clever the compiler is about this).
 
Elroch:
Good, thorough answer! In the last example, I would take the view that if you are never going to return anything, using void as the return type is more appropriate than using int. This makes the code clearer and possibly slightly more efficient (depends how clever the compiler is about this).
Thank you. I was just trying to illustrate that even if a function is defined as int an int doesn't have to be returned . . . but you are correct, where a return value is not needed void should be used.
 
RaptorUK:

In general the only stupid question is the one that goes un-asked . . . (there are exceptions to this rule, trolls, etc ;-) )

OK, I'll assume you know nothing even though I know that not the case . . . :-)

When a function is declared (the first line of a function) it is given a type . . . void, int, string, bool, double, etc . . . this means that the function has an overall resultant value . . except for type void. So when the function comes to an end, either at the last line of it's code or before that, for some particular reason, it usually has to pass back a value of it's type - this is what return(type) does. I said "usually has to" . . because if the function is called without assigning it's resultant value to another variable it can simply return; examples of this are the 3 standard functions, init() start() and deinit() they are all type int but their return values are not assigned to a variable so there is no need to use return(int);

some simple examples:

So can we say that a return; and a return(0) are actually doing the same thing if it is part of a start() function?

And am I correct in saying that it returns the control to the first line under the start() function?

I know this is VERY basic, but I do not want to assume things. Just want to confirm. I suppose I lost my confidence after a week of not being able to sort out the bugs in my EA code. Now I am questioning everything - even the basic or obvious.

 
ernest02:

1. So can we say that a return; and a return(0) are actually doing the same thing if it is part of a start() function?

2. And am I correct in saying that it returns the control to the first line under the start() function?

3. I know this is VERY basic, but I do not want to assume things. Just want to confirm. I suppose I lost my confidence after a week of not being able to sort out the bugs in my EA code. Now I am questioning everything - even the basic or obvious.

  1. Yes.
  2. Technically speaking, no. But effectively yes. Something, somewhere will call start(), we don't have visibility of this . . so the return in the start() function returns to whatever called start() then start() gets called again on the next tick.
  3. It's good to question things, very healthy, . . . "assumption is the Mother of all cock-ups"
 
RaptorUK:

  1. Yes.
  2. Technically speaking, no. But effectively yes. Something, somewhere will call start(), we don't have visibility of this . . so the return in the start() function returns to whatever called start() then start() gets called again on the next tick.
  3. It's good to question things, very healthy, . . . "assumption is the Mother of all cock-ups"

Thanks again for your assistance RaptorUK!

I feel a little bit stupid for having asked this question, but at least there are less grey areas now. Before I thought I knew - now at least I confirmed my "suspicions" of what this operator actually does! :-)

 
ernest02:

Thanks again for your assistance RaptorUK!

I feel a little bit stupid for having asked this question,

Don't . . . it's what this Forum is for . . you never know, a new user may come along one day with a similar question and use the search and find this thread and be helped because you asked the question . . . but maybe expecting new users to use the search is too much of a stretch LOL
 
ernest02:

1) So can we say that a return; and a return(0) are actually doing the same thing if it is part of a start() function?

2) And am I correct in saying that it returns the control to the first line under the start() function?

1) Yes

2) Absolutely NO!! it returns exactly after the line in your code where you have called the function.

Example:

void func1(){
  static bool isInited=false;
  if(!isInited){
   isInited=true;
    func2();
  }else{
    return;
  }

}

void func2(){
  func3();
  func1();
  return;
}

void func3(){
  return;
}


void start(){
  func1();

return; //exits the start function. Need a new tick to be recalled.
}

The execution would be like this:

  • Start()
  • call func1()
  • !isInited==true: call func2()
  • call func3()
  • return statement, returns to func2
  • call func1()
  • !isIntited==false: return statement, return to func2
                      • return statment: return to func1()
                      • return statement: return to Start()
                      • return statment: end program execution
                      This would be the calling diagram for the first tick



                     
                    Question 2 was specifically about return from start()