MQL5 Language: #define SWAP

 

Hi!

In C programming language, one way to swap numbers is:

#define swap(a,b) (a=a+b;b=a-b;a=a-b;)
How can we write this in MQL5?
 
#define swap(a,b) {a=a+b;b=a-b;a=a-b;}
 
fxsaber:
Thank you.
 
Arthur Albano:
Thank you.
I had a compiling error because I didn't see I defined a function called swap elsewhere.
 
Arthur Albano:
I had a compiling error becouse I didn't see I defined a function called swap elsewhere.
#define swap(a,b) {a=a+b;b=a-b;a=a-b;}

void OnStart()
{
  int Num1 = 0;
  int Num2 = 1;
  
  swap(Num1, Num2)
}
 

Also, sorting three numbers:

#define SORT(a,b,c) {(a > b) ? SWAP(a,b) : ((a > c) ? SWAP(a,c) : ((b>c) : SWAP(b,c)))}
 
Arthur Albano:

Also, sorting three numbers:

Such designs are impossible.

 
fxsaber:

Such designs are impossible.

#define SORT(a,b,c) {if(a > b) SWAP(a,b); if(a > c) { SWAP(a,c) }; if (b>c) { SWAP(b,c) }}
 
Yes, it works.
#define SWAP(A, B) { A += B; B = A - B; A -= B; }
#define SORT(a,b,c) {if(a > b) SWAP(a,b) if(a > c) SWAP(a,c) if (b>c) SWAP(b,c) }
 
fxsaber:
Yes, it works.

I like semicolons because it improves readability :P But thanks!