ArgMethod

Applies to:Fast Math Parser
Class:uCalc.Callback
Returns the value of an argument passed to a uCalc.Callback function
Syntax
Arg(Index)
Parameters
Index
Int32
Index of the given argument. Note that Index is 1-based; the Index for the first arg is 1 (not 0)
Returns
Type: double
Returns the value of an argument passed to a uCalc.Callback function
Remarks
When the name of a callback function is specified in DefineFunction or DefineOperator, that callback function will receive arguments uCalc passes to it through the Arg. Arg handles values of the default Double floating point data type. For other data types, ArgInt32, ArgStr, etc instead.

Note that Index is 1-based. So if there are 3 arguments, the Index of the first will be 1 (not 0) , and the second and third will be 2 and 3 respectively.

If you are defining a binary operator, 1 is the index for the left operand, and 2 for the right operand. For a unary operator, the index is 1.

Example 1: Sending arguments to a callback function

Sub AreaCB(ByVal ExprPartPtr As IntPtr)
   Dim ExprPart As New uCalc.Callback(ExprPartPtr)
   ExprPart.ReturnDbl(ExprPart.Arg(1) * ExprPart.Arg(2))
End Sub

Sub Area()
   uc.DefineFunction("Area(x, y)", uc.PinAddr(AddressOf AreaCB))
   Console.WriteLine(uc.Eval("Area(3, 4)")) ' Returns 12
End Sub

          

static void AreaCB(IntPtr ExprPartPtr) {
   var ExprPart = New uCalc.Callback(ExprPartPtr);
   ExprPart.Return(ExprPart.Arg(1) * ExprPart.Arg(2));
}

static void Area() {
   uc.DefineFunction("Area(x, y)", uc.PinAddr(AreaCB));
   Console.WriteLine(uc.Eval("Area(3, 4)")); // Returns 12;
}

          

procedure AreaCB(ExprPartPtr: System.Pointer);
begin
   
//var ExprPart: uCalc.CreateCallback(ExprPartPtr);
   ExprPart.Return(ExprPart.Arg(1) * ExprPart.Arg(2));
End;

procedure Area();
begin
   
   uc.DefineFunction('Area(x, y)', AreaCB);
   WriteLn(uc.Eval('Area(3, 4)')); // Returns 12;
End;

          

void _stdcall AreaCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   ExprPart.Return(ExprPart.Arg(1) * ExprPart.Arg(2));
}

void Area() {
   uc.DefineFunction("Area(x, y)", AreaCB);
   cout << uc.Eval("Area(3, 4)") << endl; // Returns 12;
}

          

static void AreaCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   ExprPart.Return(ExprPart.Arg(1) * ExprPart.Arg(2));
}

static void Area() {
   uc.DefineFunction("Area(x, y)", ucPinAddr(AreaCB));
   Console::WriteLine(uc.Eval("Area(3, 4)")); // Returns 12;
}

          
Example 2: Determining the number of arguments being passed to a callback function

Sub MyAverageCB(ByVal ExprPartPtr As IntPtr)
   Dim ExprPart As New uCalc.Callback(ExprPartPtr)
   Dim Total As double = 0

   Dim x As Int32
   for x = 1 To ExprPart.ArgCount()
      Total = Total + ExprPart.Arg(x)
   Next

   ExprPart.ReturnDbl(Total / ExprPart.ArgCount())
End Sub

Sub MyAverage()
   uc.DefineFunction("Average(x ...)", uc.PinAddr(AddressOf MyAverageCB))
   Console.WriteLine(uc.Eval("Average(10, 3, 7, 4)")) ' Returns 6
End Sub

          

static void MyAverageCB(IntPtr ExprPartPtr) {
   var ExprPart = New uCalc.Callback(ExprPartPtr);
   double Total = 0;
   
   for (Int32 x = 1; x <= ExprPart.ArgCount(); x++) {
      Total = Total + ExprPart.Arg(x);
   }
   
   ExprPart.Return(Total / ExprPart.ArgCount());
}

static void MyAverage() {
   uc.DefineFunction("Average(x ...)", uc.PinAddr(MyAverageCB));
   Console.WriteLine(uc.Eval("Average(10, 3, 7, 4)")); // Returns 6;
}

          

procedure MyAverageCB(ExprPartPtr: System.Pointer);
begin
   
//var ExprPart: uCalc.CreateCallback(ExprPartPtr);
//var Total: Double = 0;

   for x := 1 to ExprPart.ArgCount() do
begin
   Total = Total + ExprPart.Arg(x);
End;

   ExprPart.Return(Total / ExprPart.ArgCount());
End;

procedure MyAverage();
begin
   
   uc.DefineFunction('Average(x ...)', MyAverageCB);
   WriteLn(uc.Eval('Average(10, 3, 7, 4)')); // Returns 6;
End;

          

void _stdcall MyAverageCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   double Total = 0;
   
   for (int x = 1; x <= ExprPart.ArgCount(); x++) {
      Total = Total + ExprPart.Arg(x);
   }
   
   ExprPart.Return(Total / ExprPart.ArgCount());
}

void MyAverage() {
   uc.DefineFunction("Average(x ...)", MyAverageCB);
   cout << uc.Eval("Average(10, 3, 7, 4)") << endl; // Returns 6;
}

          

static void MyAverageCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   double Total = 0;
   
   for (int x = 1; x <= ExprPart.ArgCount(); x++) {
      Total = Total + ExprPart.Arg(x);
   }
   
   ExprPart.Return(Total / ExprPart.ArgCount());
}

static void MyAverage() {
   uc.DefineFunction("Average(x ...)", ucPinAddr(MyAverageCB));
   Console::WriteLine(uc.Eval("Average(10, 3, 7, 4)")); // Returns 6;
}

          
DLL import code
<DllImport(uCalcDLL, CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl, EntryPoint:="Arg")> _

Private Function Arg__(ByVal ExprPartHandle As IntPtr,ByVal Index As Int32) As  Double
End Function
            
[DllImport(uCalcDLL, CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl, EntryPoint="Arg")]

protected static extern  double Arg_(IntPtr ExprPartHandle, Int32 Index);
            
{DLLImport}function Arg__(ExprPartHandle: System.Pointer;Index: Int32):  Double; cdecl; external uCalcDLL name 'Arg';

            
typedef  double (* __Arg)(void *ExprPartHandle, int32_t Index); 

            
[DllImport(uCalcLib, CharSet=CharSet::Ansi, CallingConvention=CallingConvention::Cdecl, EntryPoint = "Arg")]

static double Arg_(void *  ExprPartHandle, Int32 Index);
            
See also
Prev | Next