special functions

 

Hello MQL4 community,

All statements below are regarding EAs only.

Special functions including-

init(),deinit() /*and*/ start()

are integer data-type. Each special function has a concluding function, return(0).

Why does return() function return zero by default (obviously it must return an integer datatype due to linked special function) but why must return() function return an integer at all?


Also, I've never witnessed any functions coded within special functions init() or deinit() and their pair partner return(0); within many EAs.

Can anyone in the MQL4 community provide a quick example of a practical set of functions coded within an init() or deinit() function? Is it practical to even code within init() or deinit() or even include init() or deinit() special functions within an EA at all?


Thank you

 
WhooDoo22:

Hello MQL4 community,

All statements below are regarding EAs only.

Special functions including-

are integer data-type. Each special function has a concluding function, return(0).

Why does return() function return zero by default (obviously it must return an integer datatype due to linked special function) but why must return() function return an integer at all?


Also, I've never witnessed any functions coded within special functions init() or deinit() and their pair partner return(0); within many EAs.

Can anyone in the MQL4 community provide a quick example of a practical set of functions coded within an init() or deinit() function? Is it practical to even code within init() or deinit() or even include init() or deinit() special functions within an EA at all?

Custom User Functions are not coded within start(), init() or deinit() . . . .   or for that matter any other function.  They may be called from within start(), init() or deinit(), for example I have a CleanUp() function which will delete the Objects I have placed on a chart,  I call Cleanup() from within deinit().  In another Indicator I periodically write information about some trend lines to a file,  the information is stored in a few arrays and I use   FileWriteArray()  to write the arrays to files,  I have this code in a function and it can be called to write the data to the file or read the data from the file.  The function is called to read the data within init()  and called to write data from within deinit().

Do you need more examples ?

 

Return from start() or init() or deinit() usually returns 0 because these functions are usually of type int,  if a function is of type  void  you can simply use return;

 
WhooDoo22:
Why does return() function return zero by default (obviously it must return an integer datatype due to linked special function) but why must return() function return an integer at all?
  1. return is NOT a function. It is a statement. just like continue or break;
  2. It does not return zero by default. It returns what ever you put in the parentheses (converted to the correct data type.)
  3. Good coding style dictates the you return something from non-void functions. If you fall off the end of a function without a return(v) you return a random value (whatever happens to be in the registers/memory at the time.)
  4. Since the return value from init/deinit/start is never used, common practice is to just return a zero. (They were probably declared an int because of the "C" original definition of start.)

 
RaptorUK:

Custom User Functions are not coded within start(), init() or deinit() . . . .   or for that matter any other function.  They may be called from within start(), init() or deinit(), for example I have a CleanUp() function which will delete the Objects I have placed on a chart,  I call Cleanup() from within deinit().  In another Indicator I periodically write information about some trend lines to a file,  the information is stored in a few arrays and I use   FileWriteArray()  to write the arrays to files,  I have this code in a function and it can be called to write the data to the file or read the data from the file.  The function is called to read the data within init()  and called to write data from within deinit().

Do you need more examples ?

 

Return from start() or init() or deinit() usually returns 0 because these functions are usually of type int,  if a function is of type  void  you can simply use return;

"Do you need more examples ?"

Nah, you've provided ample examples. Thanks so much for providing them. I don't know where you got "Custom User Functions" from but ;) if you'd explain why you wrote that I'd be obliged.


Thank you

 
WHRoeder:
  1. return is NOT a function. It is a statement. just like continue or break;
  2. It does not return zero by default. It returns what ever you put in the parentheses (converted to the correct data type.)
  3. Good coding style dictates the you return something from non-void functions. If you fall off the end of a function without a return(v) you return a random value (whatever happens to be in the registers/memory at the time.)
  4. Since the return value from init/deinit/start is never used, common practice is to just return a zero. (They were probably declared an int because of the "C" original definition of start.)

Excellent response William, very helpful.


Thank you

 
WhooDoo22:

"Do you need more examples ?"

Nah, you've provided ample examples. Thanks so much for providing them. I don't know where you got "Custom User Functions" from but ;) if you'd explain why you wrote that I'd be obliged.

I guess I should really have said "User defined functions",  but I hope you got my meaning.
 
RaptorUK:
I guess I should really have said "User defined functions",  but I hope you got my meaning.

Yeah, its all good. Understood.


Thank you

Reason: