0

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?.

user3666197
  • 1
  • 6
  • 50
  • 92
Waiel Mahfouz
  • 31
  • 1
  • 3

4 Answers4

3

"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....

user3666197
  • 1
  • 6
  • 50
  • 92
2

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.

user3666197
  • 1
  • 6
  • 50
  • 92
rgunning
  • 568
  • 2
  • 16
  • I am quite new to writing mql code. I asked https://stackoverflow.com/questions/55930471/how-can-i-cancel-a-trade-when-another-is-open-and-keep-the-open-trade-for-a-give?noredirect=1&lq=1 but haven't been able to get far with a solution. I would appreciate if you could help me out, if possible. Someone recently put a bounty on my question too. – NotEveryDay May 13 '19 at 15:32
1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92
  • Hi user3666197, hope you are well. I've fixed a misspelling again (useful has one L). I'll be pushing a bit harder on the headings issue, since a number of people have been editing that in your posts (including moderators). I'd like you to reduce this in your new answers. When you get a moment, please open up a chat window and we can discuss. – halfer Jun 05 '17 at 10:07
0

"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

vandyand
  • 1
  • 4