DefineFunctionMethod

Applies to:uCalc Transform
Class:uCalc
Defines a function
Syntax
DefineFunction(Definition, FunctionAddress)
Parameters
Definition
String
Text containing function to be defined
FunctionAddress
uCalcCallback
(Default = 0)
Address of callback function this function is attached to
Returns
Type: Item
Defines a function
Remarks
A function definition can either be self-contained, or it may be attached to the address of a callback function in your source code.

DefineFunction("f(x) = ...") is the same as: Define("Function: ...")

Example 1: Defining a simple end-user function

uc.DefineFunction("f(x) = 2*x + 10")
Console.WriteLine(uc.Eval("3 + f(10)")) ' Returns 33
   
          

uc.DefineFunction("f(x) = 2*x + 10");
Console.WriteLine(uc.Eval("3 + f(10)")); // Returns 33;

          

         uc.DefineFunction('f(x) = 2*x + 10');
         WriteLn(uc.Eval('3 + f(10)')); // Returns 33;
   
          

uc.DefineFunction("f(x) = 2*x + 10");
cout << uc.Eval("3 + f(10)") << endl; // Returns 33;

          

uc.DefineFunction("f(x) = 2*x + 10");
Console::WriteLine(uc.Eval("3 + f(10)")); // Returns 33;

          
Example 2: Defining end-user functions

uc.DefineFunction("f(x) = x ^ 2 + 5"): ' Simple

uc.DefineFunction("g(x As Double) = x + x"): ' Overloading
uc.DefineFunction("g(x As String) As String = x + x")
uc.DefineFunction("g(x As String, y As String) As String = x + y")
uc.DefineFunction("g(x, y) = x + y")
uc.DefineFunction("q(x, y = 5, z As String = 'Hello') = x + y + Length(z)"): ' Optional args
uc.DefineFunction("Factorial(x) = iif(x>1, x*Factorial(x-1), 1)"): ' Recursion
' See additional options under Define

Console.WriteLine(uc.EvalStr("f(5)")) ' Returns "30"
Console.WriteLine(uc.EvalStr("g(5)")) ' Returns "10"
Console.WriteLine(uc.EvalStr("g('Ha')")) ' Returns "HaHa"
Console.WriteLine(uc.EvalStr("g('Hello ', 'world!')")) ' Returns "Hello world!"
Console.WriteLine(uc.EvalStr("g(5, 10)")) ' Returns "15"
Console.WriteLine(uc.EvalStr("q(10)")) ' Returns "20"
Console.WriteLine(uc.EvalStr("q(10, 20)")) ' Returns "35"
Console.WriteLine(uc.EvalStr("q(10, 20, 'Just a test 123')")) ' Returns "45"
Console.WriteLine(uc.EvalStr("Factorial(5)")) ' Returns "120"         

          

uc.DefineFunction("f(x) = x ^ 2 + 5"); // Simple

uc.DefineFunction("g(x As Double) = x + x"); // Overloading
uc.DefineFunction("g(x As String) As String = x + x");
uc.DefineFunction("g(x As String, y As String) As String = x + y");
uc.DefineFunction("g(x, y) = x + y");
uc.DefineFunction("q(x, y = 5, z As String = 'Hello') = x + y + Length(z)"); // Optional args
uc.DefineFunction("Factorial(x) = iif(x>1, x*Factorial(x-1), 1)"); // Recursion
// See additional options under Define

Console.WriteLine(uc.EvalStr("f(5)")); // Returns "30";
Console.WriteLine(uc.EvalStr("g(5)")); // Returns "10";
Console.WriteLine(uc.EvalStr("g('Ha')")); // Returns "HaHa";
Console.WriteLine(uc.EvalStr("g('Hello ', 'world!')")); // Returns "Hello world!";
Console.WriteLine(uc.EvalStr("g(5, 10)")); // Returns "15";
Console.WriteLine(uc.EvalStr("q(10)")); // Returns "20";
Console.WriteLine(uc.EvalStr("q(10, 20)")); // Returns "35";
Console.WriteLine(uc.EvalStr("q(10, 20, 'Just a test 123')")); // Returns "45";
Console.WriteLine(uc.EvalStr("Factorial(5)")); // Returns "120";         

          

         uc.DefineFunction('f(x) = x ^ 2 + 5'); // Simple

         uc.DefineFunction('g(x As Double) = x + x'); // Overloading
         uc.DefineFunction('g(x As String) As String = x + x');
         uc.DefineFunction('g(x As String, y As String) As String = x + y');
         uc.DefineFunction('g(x, y) = x + y');
         uc.DefineFunction('q(x, y = 5, z As String = "Hello") = x + y + Length(z)'); // Optional args
         uc.DefineFunction('Factorial(x) = iif(x>1, x*Factorial(x-1), 1)'); // Recursion
         // See additional options under Define

         WriteLn(uc.EvalStr('f(5)')); // Returns '30';
         WriteLn(uc.EvalStr('g(5)')); // Returns '10';
         WriteLn(uc.EvalStr('g("Ha")')); // Returns 'HaHa';
         WriteLn(uc.EvalStr('g("Hello ", "world!")')); // Returns 'Hello world!';
         WriteLn(uc.EvalStr('g(5, 10)')); // Returns '15';
         WriteLn(uc.EvalStr('q(10)')); // Returns '20';
         WriteLn(uc.EvalStr('q(10, 20)')); // Returns '35';
         WriteLn(uc.EvalStr('q(10, 20, "Just a test 123")')); // Returns '45';
         WriteLn(uc.EvalStr('Factorial(5)')); // Returns '120';         

          

uc.DefineFunction("f(x) = x ^ 2 + 5"); // Simple

uc.DefineFunction("g(x As Double) = x + x"); // Overloading
uc.DefineFunction("g(x As String) As String = x + x");
uc.DefineFunction("g(x As String, y As String) As String = x + y");
uc.DefineFunction("g(x, y) = x + y");
uc.DefineFunction("q(x, y = 5, z As String = 'Hello') = x + y + Length(z)"); // Optional args
uc.DefineFunction("Factorial(x) = iif(x>1, x*Factorial(x-1), 1)"); // Recursion
// See additional options under Define

cout << uc.EvalStr("f(5)") << endl; // Returns "30";
cout << uc.EvalStr("g(5)") << endl; // Returns "10";
cout << uc.EvalStr("g('Ha')") << endl; // Returns "HaHa";
cout << uc.EvalStr("g('Hello ', 'world!')") << endl; // Returns "Hello world!";
cout << uc.EvalStr("g(5, 10)") << endl; // Returns "15";
cout << uc.EvalStr("q(10)") << endl; // Returns "20";
cout << uc.EvalStr("q(10, 20)") << endl; // Returns "35";
cout << uc.EvalStr("q(10, 20, 'Just a test 123')") << endl; // Returns "45";
cout << uc.EvalStr("Factorial(5)") << endl; // Returns "120";         

          

uc.DefineFunction("f(x) = x ^ 2 + 5"); // Simple

uc.DefineFunction("g(x As Double) = x + x"); // Overloading
uc.DefineFunction("g(x As String) As String = x + x");
uc.DefineFunction("g(x As String, y As String) As String = x + y");
uc.DefineFunction("g(x, y) = x + y");
uc.DefineFunction("q(x, y = 5, z As String = 'Hello') = x + y + Length(z)"); // Optional args
uc.DefineFunction("Factorial(x) = iif(x>1, x*Factorial(x-1), 1)"); // Recursion
// See additional options under Define

Console::WriteLine(uc.EvalStr("f(5)")); // Returns "30";
Console::WriteLine(uc.EvalStr("g(5)")); // Returns "10";
Console::WriteLine(uc.EvalStr("g('Ha')")); // Returns "HaHa";
Console::WriteLine(uc.EvalStr("g('Hello ', 'world!')")); // Returns "Hello world!";
Console::WriteLine(uc.EvalStr("g(5, 10)")); // Returns "15";
Console::WriteLine(uc.EvalStr("q(10)")); // Returns "20";
Console::WriteLine(uc.EvalStr("q(10, 20)")); // Returns "35";
Console::WriteLine(uc.EvalStr("q(10, 20, 'Just a test 123')")); // Returns "45";
Console::WriteLine(uc.EvalStr("Factorial(5)")); // Returns "120";         

          
Example 3: 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 4: 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;
}

          
Example 5: Displaying the data types of arguments when the data types are not known in advance

Sub DisplayArgsCB(ByVal ExprPartPtr As IntPtr)
   Dim ExprPart As New uCalc.Callback(ExprPartPtr)
   Dim x As Int32
   for x = 1 To ExprPart.ArgCount()
      Console.WriteLine(ExprPart.ArgStr(x) + "  Type: " + ExprPart.ArgObj(x).GetDataType().Name())
   Next
   ' Output
   ' 5  Type: double
   ' 3+2i  Type: complex
   ' Hello  Type: string
   ' true  Type: bool
   ' false  Type: bool
   ' 9  Type: int16
End Sub

Sub DisplayArgs()
   uc.DefineFunction("DisplayArgs(Arg As AnyType ...)", uc.PinAddr(AddressOf DisplayArgsCB))
   uc.Eval("DisplayArgs(5, 3+2*#i, 'Hello', True, False, Int16(5+4.1))")
End Sub

          

static void DisplayArgsCB(IntPtr ExprPartPtr) {
   var ExprPart = New uCalc.Callback(ExprPartPtr);
   for (Int32 x = 1; x <= ExprPart.ArgCount(); x++) {
      Console.WriteLine(ExprPart.ArgStr(x) + "  Type: " + ExprPart.ArgObj(x).GetDataType().Name());
   }
   // Output
   // 5  Type: double
   // 3+2i  Type: complex
   // Hello  Type: string
   // true  Type: bool
   // false  Type: bool
   // 9  Type: int16
   
}

static void DisplayArgs() {
   uc.DefineFunction("DisplayArgs(Arg As AnyType ...)", uc.PinAddr(DisplayArgsCB));
   uc.Eval("DisplayArgs(5, 3+2*#i, 'Hello', True, False, Int16(5+4.1))");
}

          

procedure DisplayArgsCB(ExprPartPtr: System.Pointer);
begin
   
//var ExprPart: uCalc.CreateCallback(ExprPartPtr);
   for x := 1 to ExprPart.ArgCount() do
begin
   WriteLn(ExprPart.ArgStr(x) + '  Type: ' + ExprPart.ArgObj(x).GetDataType().Name());
End;
   // Output
// 5  Type: double
// 3+2i  Type: complex
// Hello  Type: string
// true  Type: bool
// false  Type: bool
// 9  Type: int16

End;

procedure DisplayArgs();
begin
   
   uc.DefineFunction('DisplayArgs(Arg As AnyType ...)', DisplayArgsCB);
   uc.Eval('DisplayArgs(5, 3+2*#i, "Hello", True, False, Int16(5+4.1))');
End;

          

void _stdcall DisplayArgsCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   for (int x = 1; x <= ExprPart.ArgCount(); x++) {
      cout << ExprPart.ArgStr(x) << "  Type: " << ExprPart.ArgObj(x).GetDataType().Name() << endl;
   }
   // Output
   // 5  Type: double
   // 3+2i  Type: complex
   // Hello  Type: string
   // true  Type: bool
   // false  Type: bool
   // 9  Type: int16
   
}

void DisplayArgs() {
   uc.DefineFunction("DisplayArgs(Arg As AnyType ...)", DisplayArgsCB);
   uc.Eval("DisplayArgs(5, 3+2*#i, 'Hello', True, False, Int16(5+4.1))");
}

          

static void DisplayArgsCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   for (int x = 1; x <= ExprPart.ArgCount(); x++) {
      Console::WriteLine(ExprPart.ArgStr(x) + "  Type: " + ExprPart.ArgObj(x).GetDataType().Name());
   }
   // Output
   // 5  Type: double
   // 3+2i  Type: complex
   // Hello  Type: string
   // true  Type: bool
   // false  Type: bool
   // 9  Type: int16
   
}

static void DisplayArgs() {
   uc.DefineFunction("DisplayArgs(Arg As AnyType ...)", ucPinAddr(DisplayArgsCB));
   uc.Eval("DisplayArgs(5, 3+2*#i, 'Hello', True, False, Int16(5+4.1))");
}

          
Example 6: Evaluate an expression only during callback

Sub EvaluateCallbackCB(ByVal ExprPartPtr As IntPtr)
   Dim ExprPart As New uCalc.Callback(ExprPartPtr)
   Console.WriteLine(ExprPart.ArgExpr(1).Evaluate()) ' Returns 90
   Console.WriteLine(ExprPart.ArgExpr(2).EvaluateStr()) ' Returns "Hello There FRIENDS"
End Sub

Sub EvaluateCallback()
   uc.DefineFunction("f(ByExpr Ex, ByExpr ExStr As String)", uc.PinAddr(AddressOf EvaluateCallbackCB))
   uc.Eval("f(10+20*4, 'Hello' + ' There ' +UCase('Friends'))")
End Sub

          

static void EvaluateCallbackCB(IntPtr ExprPartPtr) {
   var ExprPart = New uCalc.Callback(ExprPartPtr);
   Console.WriteLine(ExprPart.ArgExpr(1).Evaluate()); // Returns 90;
   Console.WriteLine(ExprPart.ArgExpr(2).EvaluateStr()); // Returns "Hello There FRIENDS";
}

static void EvaluateCallback() {
   uc.DefineFunction("f(ByExpr Ex, ByExpr ExStr As String)", uc.PinAddr(EvaluateCallbackCB));
   uc.Eval("f(10+20*4, 'Hello' + ' There ' +UCase('Friends'))");
}

          

procedure EvaluateCallbackCB(ExprPartPtr: System.Pointer);
begin
   
//var ExprPart: uCalc.CreateCallback(ExprPartPtr);
   WriteLn(ExprPart.ArgExpr(1).Evaluate()); // Returns 90;
   WriteLn(ExprPart.ArgExpr(2).EvaluateStr()); // Returns 'Hello There FRIENDS';
End;

procedure EvaluateCallback();
begin
   
   uc.DefineFunction('f(ByExpr Ex, ByExpr ExStr As String)', EvaluateCallbackCB);
   uc.Eval('f(10+20*4, "Hello" + " There " +UCase("Friends"))');
End;

          

void _stdcall EvaluateCallbackCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   cout << ExprPart.ArgExpr(1).Evaluate() << endl; // Returns 90;
   cout << ExprPart.ArgExpr(2).EvaluateStr() << endl; // Returns "Hello There FRIENDS";
}

void EvaluateCallback() {
   uc.DefineFunction("f(ByExpr Ex, ByExpr ExStr As String)", EvaluateCallbackCB);
   uc.Eval("f(10+20*4, 'Hello' + ' There ' +UCase('Friends'))");
}

          

static void EvaluateCallbackCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   Console::WriteLine(ExprPart.ArgExpr(1).Evaluate()); // Returns 90;
   Console::WriteLine(ExprPart.ArgExpr(2).EvaluateStr()); // Returns "Hello There FRIENDS";
}

static void EvaluateCallback() {
   uc.DefineFunction("f(ByExpr Ex, ByExpr ExStr As String)", ucPinAddr(EvaluateCallbackCB));
   uc.Eval("f(10+20*4, 'Hello' + ' There ' +UCase('Friends'))");
}

          
Example 7: Dealing with various data types in a callback

Sub MyFunctionCB(ByVal ExprPartPtr As IntPtr)
   Dim ExprPart As New uCalc.Callback(ExprPartPtr)
   Dim ucc = ExprPart.GetuCalc()

   Console.WriteLine(ExprPart.ArgInt32(1)) ' Returns 1230
   Console.WriteLine(ExprPart.ArgInt64(2)) ' Returns 2

   Console.WriteLine(ucc.GetItemOf("Int8").GetDataType().ToString(ExprPart.ArgAddr(3))) ' Returns -1
   Console.WriteLine(ucc.GetItemOf("Int").GetDataType().ToString(ExprPart.ArgPtr(4))) ' Returns 123
End Sub

Sub MyFunctionCallback()
   uc.DefineVariable("x As Int = 123")
   uc.DefineVariable("xPtr As Int Ptr = AddressOf(x)")
   uc.DefineFunction("MyFunc(a As Int32, b As Int64, c As Byte, d As Int Ptr)", uc.PinAddr(AddressOf MyFunctionCB))
   uc.Eval("MyFunc(x*10, 1+1, 255, xPtr)")
End Sub

          

static void MyFunctionCB(IntPtr ExprPartPtr) {
   var ExprPart = New uCalc.Callback(ExprPartPtr);
   var ucc = ExprPart.GetuCalc();
   
   Console.WriteLine(ExprPart.ArgInt32(1)); // Returns 1230;
   Console.WriteLine(ExprPart.ArgInt64(2)); // Returns 2;
   
   Console.WriteLine(ucc.GetItemOf("Int8").GetDataType().ToString(ExprPart.ArgAddr(3))); // Returns -1;
   Console.WriteLine(ucc.GetItemOf("Int").GetDataType().ToString(ExprPart.ArgPtr(4))); // Returns 123;
}

static void MyFunctionCallback() {
   uc.DefineVariable("x As Int = 123");
   uc.DefineVariable("xPtr As Int Ptr = AddressOf(x)");
   uc.DefineFunction("MyFunc(a As Int32, b As Int64, c As Byte, d As Int Ptr)", uc.PinAddr(MyFunctionCB));
   uc.Eval("MyFunc(x*10, 1+1, 255, xPtr)");
}

          

procedure MyFunctionCB(ExprPartPtr: System.Pointer);
begin
   
//var ExprPart: uCalc.CreateCallback(ExprPartPtr);
//var ucc = ExprPart.GetuCalc();

   WriteLn(ExprPart.ArgInt32(1)); // Returns 1230;
   WriteLn(ExprPart.ArgInt64(2)); // Returns 2;

   WriteLn(ucc.GetItemOf('Int8').GetDataType().ToString(ExprPart.ArgAddr(3))); // Returns -1;
   WriteLn(ucc.GetItemOf('Int').GetDataType().ToString(ExprPart.ArgPtr(4))); // Returns 123;
End;

procedure MyFunctionCallback();
begin
   
   uc.DefineVariable('x As Int = 123');
   uc.DefineVariable('xPtr As Int Ptr = AddressOf(x)');
   uc.DefineFunction('MyFunc(a As Int32, b As Int64, c As Byte, d As Int Ptr)', MyFunctionCB);
   uc.Eval('MyFunc(x*10, 1+1, 255, xPtr)');
End;

          

void _stdcall MyFunctionCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   auto ucc = ExprPart.GetuCalc();
   
   cout << ExprPart.ArgInt32(1) << endl; // Returns 1230;
   cout << ExprPart.ArgInt64(2) << endl; // Returns 2;
   
   cout << ucc.GetItemOf("Int8").GetDataType().ToString(ExprPart.ArgAddr(3)) << endl; // Returns -1;
   cout << ucc.GetItemOf("Int").GetDataType().ToString(ExprPart.ArgPtr(4)) << endl; // Returns 123;
}

void MyFunctionCallback() {
   uc.DefineVariable("x As Int = 123");
   uc.DefineVariable("xPtr As Int Ptr = AddressOf(x)");
   uc.DefineFunction("MyFunc(a As Int32, b As Int64, c As Byte, d As Int Ptr)", MyFunctionCB);
   uc.Eval("MyFunc(x*10, 1+1, 255, xPtr)");
}

          

static void MyFunctionCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   auto ucc = ExprPart.GetuCalc();
   
   Console::WriteLine(ExprPart.ArgInt32(1)); // Returns 1230;
   Console::WriteLine(ExprPart.ArgInt64(2)); // Returns 2;
   
   Console::WriteLine(ucc.GetItemOf("Int8").GetDataType().ToString(ExprPart.ArgAddr(3))); // Returns -1;
   Console::WriteLine(ucc.GetItemOf("Int").GetDataType().ToString(ExprPart.ArgPtr(4))); // Returns 123;
}

static void MyFunctionCallback() {
   uc.DefineVariable("x As Int = 123");
   uc.DefineVariable("xPtr As Int Ptr = AddressOf(x)");
   uc.DefineFunction("MyFunc(a As Int32, b As Int64, c As Byte, d As Int Ptr)", ucPinAddr(MyFunctionCB));
   uc.Eval("MyFunc(x*10, 1+1, 255, xPtr)");
}

          
Example 8: Returning a string (from a callback)

Sub TwiceStrCB(ByVal ExprPartPtr As IntPtr)
   Dim ExprPart As New uCalc.Callback(ExprPartPtr)
   ExprPart.ReturnStr(ExprPart.ArgStr(1) + ExprPart.ArgStr(1))
End Sub

Sub TwiceStr()
   uc.DefineFunction("Twice(Txt As String) As String", uc.PinAddr(AddressOf TwiceStrCB))
   Console.WriteLine(uc.EvalStr("Twice('Ha')")) ' Returns "HaHa"
End Sub

          

static void TwiceStrCB(IntPtr ExprPartPtr) {
   var ExprPart = New uCalc.Callback(ExprPartPtr);
   ExprPart.ReturnStr(ExprPart.ArgStr(1) + ExprPart.ArgStr(1));
}

static void TwiceStr() {
   uc.DefineFunction("Twice(Txt As String) As String", uc.PinAddr(TwiceStrCB));
   Console.WriteLine(uc.EvalStr("Twice('Ha')")); // Returns "HaHa";
}

          

procedure TwiceStrCB(ExprPartPtr: System.Pointer);
begin
   
//var ExprPart: uCalc.CreateCallback(ExprPartPtr);
   ExprPart.ReturnStr(ExprPart.ArgStr(1) + ExprPart.ArgStr(1));
End;

procedure TwiceStr();
begin
   
   uc.DefineFunction('Twice(Txt As String) As String', TwiceStrCB);
   WriteLn(uc.EvalStr('Twice("Ha")')); // Returns 'HaHa';
End;

          

void _stdcall TwiceStrCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   ExprPart.ReturnStr(ExprPart.ArgStr(1) + ExprPart.ArgStr(1));
}

void TwiceStr() {
   uc.DefineFunction("Twice(Txt As String) As String", TwiceStrCB);
   cout << uc.EvalStr("Twice('Ha')") << endl; // Returns "HaHa";
}

          

static void TwiceStrCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   ExprPart.ReturnStr(ExprPart.ArgStr(1) + ExprPart.ArgStr(1));
}

static void TwiceStr() {
   uc.DefineFunction("Twice(Txt As String) As String", ucPinAddr(TwiceStrCB));
   Console::WriteLine(uc.EvalStr("Twice('Ha')")); // Returns "HaHa";
}

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

Private Function DefineFunction__(ByVal uCalcHandle As IntPtr,ByVal Definition As String , ByVal FunctionAddress As uCalcDelegate  ) As IntPtr
End Function
            
[DllImport(uCalcDLL, CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl, EntryPoint="DefineFunction")]

protected static extern IntPtr DefineFunction_(IntPtr uCalcHandle, string Definition , uCalcDelegate FunctionAddress  );
            
{DLLImport}function DefineFunction__(uCalcHandle: System.Pointer;Definition: AnsiString ; FunctionAddress: System.Pointer): System.Pointer; cdecl; external uCalcDLL name 'DefineFunction';

            
typedef uCalcPtr (* __DefineFunction)(void *uCalcHandle, CONSTCHAR Definition  ,  UCCALLBACK FunctionAddress  ); 

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

static uCalcPtr DefineFunction_(void *  uCalcHandle, MARSHALSTR Definition  ,  UCCALLBACK FunctionAddress);
            
See also
Prev | Next