MQL4 BASICS COURSE

Using void Functions for Reusable Logic (MQL4)

Lesson Goal

In this lesson, you will learn:


1) What Is a void Function?

A void function is a function that:

The word void literally means:

“This function returns nothing.”

Instead of returning a value, a void function usually:

Think of a void function as an instruction:

“Go do this job.”

2) Why void Functions Are So Important in EAs

Expert Advisors are not simple programs. They must:

If all of this logic is placed inside OnTick(), the code becomes very hard to read and very hard to fix.

void functions solve this problem by breaking the EA into small, clear action blocks.


3) The Wrong Way (Everything Inside OnTick)

Many beginners write EAs like this:

void OnTick()
{
   double spread = MarketInfo(Symbol(), MODE_SPREAD);
   if(spread > 20) return;

   if(OrdersTotal() == 0)
   {
      // Entry logic here
   }

   if(OrdersTotal() > 0)
   {
      // Trade management logic here
   }
}

This may work at first, but it has serious problems:


4) The Right Way: Using void Functions

Instead of placing logic directly inside OnTick(), we move the logic into void functions.

Each function does one job only.

Clean OnTick Example

void OnTick()
{
   CheckTradingConditions();
   ManageOpenTrades();
   LookForNewEntries();
}

Now OnTick() reads like a checklist. This is intentional.


5) Example: A Simple void Function

Here is a basic void function that checks the spread and updates a global variable.

// Global variable
bool TradingEnabled = true;

// Void function
void CheckTradingConditions()
{
   double spread = MarketInfo(Symbol(), MODE_SPREAD);

   if(spread > 20)
      TradingEnabled = false;
   else
      TradingEnabled = true;
}

This function:

Other parts of the EA can now simply check:

if(!TradingEnabled) return;

6) Reusing Logic Without Copy & Paste

Imagine you need to check trading conditions:

Without void functions, you would copy and paste the same code. This is dangerous.

With a void function, you write the logic once:

void CheckTradingConditions()
{
   // spread, time, daily limits, etc.
}

Then you reuse it anywhere:

CheckTradingConditions();

7) Full EA Example (Seeing Everything Together)

This example shows where void functions live inside a real EA file.

//==============================
// GLOBAL VARIABLES
//==============================
bool TradingEnabled = true;

//==============================
// ONTICK (very short)
//==============================
void OnTick()
{
   CheckTradingConditions();
   if(!TradingEnabled) return;

   ManageOpenTrades();
   LookForNewEntries();
}

//==============================
// VOID FUNCTIONS (action blocks)
//==============================

void CheckTradingConditions()
{
   double spread = MarketInfo(Symbol(), MODE_SPREAD);

   if(spread > 20)
      TradingEnabled = false;
   else
      TradingEnabled = true;
}

void ManageOpenTrades()
{
   // trailing stops, exits, etc.
}

void LookForNewEntries()
{
   if(OrdersTotal() > 0) return;

   // entry logic here
}

This structure is clean, readable, and easy to expand later.


8) Common Beginner Mistakes

Remember:

One function = one job.

Lesson Summary


Next Lesson

In the next lesson, we will learn:

Organizing EA Logic Outside OnTick()

You’ll see how professional EAs are structured so they stay readable, debuggable, and expandable as they grow.