Trying to pass a function by reference to another function . Any help

 

I have been trying to pass a function by reference to another function but i think i must be doing it in the wrong manner . any quick help is well appreciated 


void OnTick()
{   
     double a, b , c ;
     a = 0.0;
     b = 1;
     c = 0.0;

     // Trying to pass the function  f1_func to Function1  
     // but getting ERROR:  'f1_func' --  parameter passed by reference, variable expected.
         
     result =  Function1(f1_func,a,b,c);  // <=========== Error here 
         
     Print( "Result = " , result);
  
}

//--------------------------------------

/* Definition of an arbitrary function with parameters */

struct general_function
{
    double params;
    double function(double x, double params);
};


inline double general_func_value(general_function &F, double x)
{
    return F.function(x,F.params);
};


double Function1(general_function &F, double start,int end,double a)
{
  double   myFuncVal, param1;
  
  param1    = start + end*((1-0.35)/0.35);
  myFuncVal = 2 * general_func_value(F, parma1);
  return (myFuncVal);
  
}

//=========Function to be passed by reference to Function1 in OnTick================

double f1_func(double x, double a)
{
        return log(x)/(a + 100.0*x*x);
}
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
typedef double(*TDblFunc)(double,double);

struct SDblFunc
  {
   double            par;
   TDblFunc          function;
  };

double Dblfunc(double x, double par)
  {
   return MathPow(x,par);
  }

int OnInit()
  {
  SDblFunc sdf;
  sdf.function=Dblfunc;
  sdf.par=2.0;
  Print(sdf.function(-1.0,sdf.par)); // -> 1.0

You need to work with typedef here.

 
lippmaje:

You need to work with typedef here.

thanks that helped .