A question by an MQL4 newbie.
What are the limits of what a void
function can do in MQL4?.
I mean what can be done by a void
function code and what can not be done?.
A question by an MQL4 newbie.
What are the limits of what a void
function can do in MQL4?.
I mean what can be done by a void
function code and what can not be done?.
"void
" only means that there is no return value from such function. So "returning a value" can not be done by a void
function.
Hope that help....
you can put everything in a void
function that you can put in a double
, int
, string
, bool
, ... function. What changes is what type of variable the function returns.
For instance, the following int
function returns the sum of two values.
int sum( int a, int b )
{
return( a + b );
}
you could turn this function into a void
function and instead of returning the value, you can print the value to the console.
void printsum( int a, int b )
{
Print( a + b );
}
In your follow up answer you ask about creating a void
function that does something to a moving average. The following void
function will accept different periods as input and print the MA. The function can't directly return the value of anything ( unless you use global variables / pass variables by reference ), but it can still accept values and do stuff based on those values.
void PrintMA( int period )
{
Print( iMA( NULL, 0, period, 8, MODE_SMMA, PRICE_MEDIAN, 1 ) );
}
The int
function in your follow up answer only ever returns 0, so you could swap it to a void
function and remove return(0)
and it will work as before. Just change the function name first as start
is a function name you should avoid using.
If you read the compile log, you'll be able to see why your above answer won't compile.
The only thing a void function(...)
cannot do is to ever participate in an MQL4 assignment statement, i.e.:
someVariable = aVoidDeclaredFUNCTION();
Except this, one can do literally everything imaginable.
How that can be useful?
void aVoidDeclaredFUNCTION( const int thisParameterWillNeverChangeItsVALUE,
int &thisParameterWillBeAbleToChangeVALUE
){...}
Using a technique to pass by-Value, resp. to pass by-reference ( &passVariableByREF
) , even a void function(...)
can process and "return"-results, if it is not enough to cause some actions in the void function(...){...}
body, per-se.
"Void" just means the function doesn't return anything. These are useful for segmenting any stand alone sections of code (to make the code more organized for example, or to prevent repeating code... etc).
See this short video (not made by me) on the topic: Void Functions