1.2.4 Functions


Global Functions

Origin C provides many global functions for performing a variety of tasks. These functions fall into twenty-six categories:

  1. Basic IO
  2. Character and String Manipulation
  3. COM
  4. Communications
  5. Curve
  6. Data Conversion
  7. Data Range
  8. Date Time
  9. File IO
  10. File Management
  11. Fitting
  12. Image Processing
  13. Import Export
  14. Internal Origin Objects
  15. LabTalk Interface
  16. Math Functions
  17. Mathematics
  18. Matrix Conversion and Gridding
  19. Memory Management
  20. NAG
  21. Signal Processing
  22. Spectroscopy
  23. Statistics
  24. System
  25. Tree
  26. User Interface

Please refer to the Global Functions section for a complete list of functions with examples.

User-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

For details on which signatures are callable from script and how to keep functions available, see

the LabTalk help on calling Origin C functions.