1.2.4 Functions
Global FunctionsGlobal Functions
Origin C provides many global functions Functions, Predefined for performing a variety of tasks. These functions fall into twenty-six categories:
- Basic IO
- Character and String Manipulation
- COM
- Communications
- Curve
- Data Conversion
- Data Range
- Date Time
- File IO
- File Management
- Fitting
- Image Processing
- Import Export
- Internal Origin Objects
- LabTalk Interface
- Math Functions
- Mathematics
- Matrix Conversion and Gridding
- Memory Management
- NAG
- Signal Processing
- Spectroscopy
- Statistics
- System
- Tree
- User Interface
Please refer to the Global Functions section for a complete list of functions with examples.
Functions, User-definedUser-Defined Functions
Origin C supports user-defined functions. A user-defined function lets you create functions that accept your choice of arguments and return type, and operate on those arguments to achieve a specific purpose.
Minimal example (pass by value)
Define the function and call it in one block:
double my_function(double dData)
{
dData += 10;
return dData;
}
// Usage
double d = 3.3;
d = my_function(d);
out_double("d == ", d); // 13.3
Modify a value (pass by reference)
Use a reference parameter to modify the caller’s variable without returning a value.
void add_offset(double& x, double offset)
{
x += offset; // modifies 'x' in the caller
}
// Usage
double v = 5;
add_offset(v, 2.5);
out_double("v == ", v); // 7.5
Overloading and default arguments
Origin C supports function overloading and default parameter values.
// Two overloads for scale(): vector<double> and vector<int>
void scale(vector<double>& v, double factor = 1.0)
{
int n = v.GetSize();
for (int i = 0; i < n; i++)
v[i] *= factor;
}
void scale(vector<int>& v, double factor = 1.0)
{
int n = v.GetSize();
for (int i = 0; i < n; i++)
v[i] = (int)(v[i] * factor);
}
// Usage
vector<double> a = {1.0, 2.0, 3.0};
scale(a, 2.0); // -> {2, 4, 6}
vector<int> b = {1, 2, 3};
scale(b); // default factor 1.0 (no change)
Copy-return variant (read-only input)
Keep the input read-only and return a scaled copy.
vector<double> scale(const vector<double>& v, double factor)
{
vector<double> out = v; // copy input
int n = out.GetSize();
for (int i = 0; i < n; i++)
out[i] *= factor;
return out;
}
// Usage
vector<double> c = {2.5, 4.0};
vector<double> d2 = scale(c, 0.5); // c unchanged, d2 = {1.25, 2.0}
Calling from LabTalk
You can call eligible Origin C functions from LabTalk (parameters/return types must meet certain criteria).
// Example function callable from LabTalk:
double add_then_scale(double x, double y, double factor)
{
return (x + y) * factor;
}
// In the Script Window or LabTalk Console:
double r;
r = add_then_scale(2.5, 3.5, 10); // calls the Origin C function
r=; // outputs 60
|