When to use Return Operator?

 

What's the different between:

return(1);

return(0);

return(-1);

return;

thank you

 
Hi,

With "return" you indicate that you want to return to the caller of the currently active function, possibly yielding a so-called return value.
The first three instances return an integer, the fourth instance doesn't return any value.

Cheers,

Max
 

Hi MaxPayne, thank you for your explanation.


In delphi there is BREAK operator to jump out from the current loop, EXIT operator to exit current procedure or function, or RESULT operator if i'm using function.

RETURN operator i think has same meaning with the RESULT operator in delphi.


In MQL4 (C), at the end of init, deinit and start built in function there's RETURN(0). Is it mean EXIT? I think we're already at the last line of current function. Why should we put RETURN(0) at the last line?

Or is it mean current function is SUCCEED or generate no error value? In some case, when GetLastError() return an error value, I found that there's always Return(-1) operator.

And when to use RETURN(1) operator?

Thank you.

 
You use it when you need some return of value, if you don't need any return you can use void _function() { /* ... */ } and without return; at all, or use return as break operator for the main/all function. If you need return of a double value ( 1. ) you need function defined as double _function() {}, if you need string as return, you need function defined as string _function() {}.
If you need to change parameter's values, you are defining them in initializing ex.: _function( double& double_one, double& double_two ) {}, in this case both values are returned back and changed.