How to use a local variable in another function MQL4/MQL5/C++

 
I am trying to use my variable switch_action which is computed in the zmq code (InterpretZmqMessage) and use it in my second function (secondfunction)

#property strict
#include <stdlib.mqh>
#include <stderror.mqh>
#include <WinUser32.mqh>
datetime LastActiontime;
#import "shell32.dll"
int ShellExecuteW(int hwnd,const string Operation,const string File,const string Parameters,const string Directory,int ShowCmd);
#import
// Required: MQL-ZMQ from https://github.com/dingmaotu/mql-zmq
#include <Zmq/Zmq.mqh>
//A.I stuff here
extern string PROJECT_NAME="Ai";
extern string ZEROMQ_PROTOCOL="tcp";
extern string HOSTNAME="127.0.0.1";
extern int REP_PORT=5555;
extern int PUSH_PORT=5556;
extern int MILLISECOND_TIMER=1;  // 1 millisecond
                                 //#include <stdlib.mqh>
#import "stdlib.ex4"
string ErrorDescription(int a0); // DA69CBAFF4D38B87377667EEC549DE5A
#import

// CREATE ZeroMQ Context
Context context(PROJECT_NAME);

// CREATE ZMQ_REP SOCKET
Socket repSocket(context,ZMQ_REP);

// CREATE ZMQ_PUSH SOCKET
Socket pushSocket(context,ZMQ_PUSH);

// VARIABLES FOR LATER
uchar data[];
ZmqMsg request;

int init() 
  {

   EventSetMillisecondTimer(MILLISECOND_TIMER);     // Set Millisecond Timer to get client socket input

   Print("[REP] Binding MT4 Server to Socket on Port "+REP_PORT+"..");
   Print("[PUSH] Binding MT4 Server to Socket on Port "+PUSH_PORT+"..");

   repSocket.bind(StringFormat("%s://%s:%d",ZEROMQ_PROTOCOL,HOSTNAME,REP_PORT));
   pushSocket.bind(StringFormat("%s://%s:%d",ZEROMQ_PROTOCOL,HOSTNAME,PUSH_PORT));

/*
       Maximum amount of time in milliseconds that the thread will try to send messages 
       after its socket has been closed (the default value of -1 means to linger forever):
   */

   repSocket.setLinger(1000);  // 1000 milliseconds

/* 
      If we initiate socket.send() without having a corresponding socket draining the queue, 
      we'll eat up memory as the socket just keeps enqueueing messages.

      So how many messages do we want ZeroMQ to buffer in RAM before blocking the socket?
   */

   repSocket.setSendHighWaterMark(5);     // 5 messages only.

// 52D46093050F38C27267BCE42543EF60
int deinit() 
  {
   Print("[REP] Unbinding MT4 Server from Socket on Port "+REP_PORT+"..");
   repSocket.unbind(StringFormat("%s://%s:%d",ZEROMQ_PROTOCOL,HOSTNAME,REP_PORT));

   Print("[PUSH] Unbinding MT4 Server from Socket on Port "+PUSH_PORT+"..");
   pushSocket.unbind(StringFormat("%s://%s:%d",ZEROMQ_PROTOCOL,HOSTNAME,PUSH_PORT));
   return (0);
  }

    void OnTimer()
      {
    //---

    /*
          For this example, we need:
          1) socket.recv(request,true)
          2) MessageHandler() to process the request
          3) socket.send(reply)
       */

    // Get client's response, but don't wait.
       repSocket.recv(request,true);

    // MessageHandler() should go here.   
       ZmqMsg reply=MessageHandler(request);

    // socket.send(reply) should go here.
       repSocket.send(reply);
      }
    //+------------------------------------------------------------------+

    ZmqMsg MessageHandler(ZmqMsg &request) 
      {

    // Output object
       ZmqMsg reply;

    // Message components for later.
       string components[];

       if(request.size()>0) 
         {

          // Get data from request   
          ArrayResize(data,request.size());
          request.getData(data);
          string dataStr=CharArrayToString(data);

          // Process data
          ParseZmqMessage(dataStr,components);

          // Interpret data
          InterpretZmqMessage(&pushSocket,components);

          // Construct response
          ZmqMsg ret(StringFormat("[SERVER] Processing: %s",dataStr));
          reply=ret;

         }
       else 
         {
          // NO DATA RECEIVED
         }

       return(reply);
      }
    // Interpret Zmq Message and perform actions
void InterpretZmqMessage(Socket &pSocket,string &compArray[]) 
  {

   Print("ZMQ: Interpreting Message..");


   int switch_action=99;

   if(compArray[0]=="buy")
      switch_action= 1;
   if(compArray[0] == "sell")
      switch_action= 0;
   if(compArray[0] == "close")
      switch_action= 4;
   if(compArray[0] == "data666")
      switch_action= 3;


  }







int start()
{secondfunction();
}

int secondfunction() 
  {
  int switch_action;
   if(DayOfWeek()<1) 
     {
         if((switch_action)==0)
            checktrade=TRUE;

         else
            checktrade=FALSE;

         }
   return (checktrade);
  }

void ParseZmqMessage(string &message,string &retArray[]) 
  {

   Print("Parsing: "+message);

   string sep="|";
   ushort u_sep=StringGetCharacter(sep,0);

   int splits=StringSplit(message,u_sep,retArray);

   for(int i=0; i<splits; i++) 
     {
      Print(i+") "+retArray[i]);
     }
  }

I have tried global variables by creating them in the zmq function and using them in the second function but aside from being poor coding practice, they led to further issues such as my other functions not working.

For this problem, I was researching into pointers and tried to implement them but to my avail, they did not work. I apologize, I am relatively new to this language so it is difficult for me to solve this problem on my own.

 
QuantCoder:
I am trying to use my variable switch_action which is computed in the zmq code (InterpretZmqMessage) and use it in my second function (secondfunction)


I have tried global variables by creating them in the zmq function and using them in the second function but aside from being poor coding practice, they led to further issues such as my other functions not working.

For this problem, I was researching into pointers and tried to implement them but to my avail, they did not work. I apologize, I am relatively new to this language so it is difficult for me to solve this problem on my own.

I don't think you have enough threads open to discuss this topic. Perhaps you'd get a better answer if you open more...