Shows how to create overloaded functions that uCalc distinguishes based on parameter count and type.
ID: 298
See: DefineFunction
using uCalcSoftware;
var uc = new uCalc();
// Overload for two numbers
uc.DefineFunction("Combine(x, y) = x + y");
// Overload for two strings
uc.DefineFunction("Combine(x As String, y As String) As String = x + y");
// Overload for three numbers
uc.DefineFunction("Combine(x, y, z) = x + y + z");
Console.WriteLine($"Two numbers: {uc.EvalStr("Combine(5, 10)")}");
Console.WriteLine($"Two strings: {uc.EvalStr("Combine('Hello, ', 'World!')")}");
Console.WriteLine($"Three numbers: {uc.EvalStr("Combine(5, 10, 20)")}");
Two numbers: 15
Two strings: Hello, World!
Three numbers: 35 using uCalcSoftware; var uc = new uCalc(); // Overload for two numbers uc.DefineFunction("Combine(x, y) = x + y"); // Overload for two strings uc.DefineFunction("Combine(x As String, y As String) As String = x + y"); // Overload for three numbers uc.DefineFunction("Combine(x, y, z) = x + y + z"); Console.WriteLine($"Two numbers: {uc.EvalStr("Combine(5, 10)")}"); Console.WriteLine($"Two strings: {uc.EvalStr("Combine('Hello, ', 'World!')")}"); Console.WriteLine($"Three numbers: {uc.EvalStr("Combine(5, 10, 20)")}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Overload for two numbers
uc.DefineFunction("Combine(x, y) = x + y");
// Overload for two strings
uc.DefineFunction("Combine(x As String, y As String) As String = x + y");
// Overload for three numbers
uc.DefineFunction("Combine(x, y, z) = x + y + z");
cout << "Two numbers: " << uc.EvalStr("Combine(5, 10)") << endl;
cout << "Two strings: " << uc.EvalStr("Combine('Hello, ', 'World!')") << endl;
cout << "Three numbers: " << uc.EvalStr("Combine(5, 10, 20)") << endl;
}
Two numbers: 15
Two strings: Hello, World!
Three numbers: 35 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Overload for two numbers uc.DefineFunction("Combine(x, y) = x + y"); // Overload for two strings uc.DefineFunction("Combine(x As String, y As String) As String = x + y"); // Overload for three numbers uc.DefineFunction("Combine(x, y, z) = x + y + z"); cout << "Two numbers: " << uc.EvalStr("Combine(5, 10)") << endl; cout << "Two strings: " << uc.EvalStr("Combine('Hello, ', 'World!')") << endl; cout << "Three numbers: " << uc.EvalStr("Combine(5, 10, 20)") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Overload for two numbers
uc.DefineFunction("Combine(x, y) = x + y")
'// Overload for two strings
uc.DefineFunction("Combine(x As String, y As String) As String = x + y")
'// Overload for three numbers
uc.DefineFunction("Combine(x, y, z) = x + y + z")
Console.WriteLine($"Two numbers: {uc.EvalStr("Combine(5, 10)")}")
Console.WriteLine($"Two strings: {uc.EvalStr("Combine('Hello, ', 'World!')")}")
Console.WriteLine($"Three numbers: {uc.EvalStr("Combine(5, 10, 20)")}")
End Sub
End Module
Two numbers: 15
Two strings: Hello, World!
Three numbers: 35 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Overload for two numbers uc.DefineFunction("Combine(x, y) = x + y") '// Overload for two strings uc.DefineFunction("Combine(x As String, y As String) As String = x + y") '// Overload for three numbers uc.DefineFunction("Combine(x, y, z) = x + y + z") Console.WriteLine($"Two numbers: {uc.EvalStr("Combine(5, 10)")}") Console.WriteLine($"Two strings: {uc.EvalStr("Combine('Hello, ', 'World!')")}") Console.WriteLine($"Three numbers: {uc.EvalStr("Combine(5, 10, 20)")}") End Sub End Module