Download Help (Windows Only) |

You cannot execute MathScript functions directly. In other words, you first must define and save a function before you can call the function with inputs and outputs in the **LabVIEW MathScript Window** or the MathScript Node. A LabVIEW MathScript function must use the following syntax:

%

- Begin each function definition with function.
- Define the output variables for
*outputs*. If the function has more than one output variable, enclose the variables in square brackets and separate the variables with white space or commas. - Define the name of the function for
*function_name*. - Define the input variables for
*inputs*. Use commas to separate the input variables. - Create help content for
*documentation*. LabVIEW displays this help content for the function when you execute the help command in the**LabVIEW MathScript Window**or move the cursor over the function in a MathScript Node. Precede each line of help documentation with a % character. You can place help content anywhere in the function definition. However, LabVIEW displays only the first group of commented lines in the**Output Window**of the**LabVIEW MathScript Window**and the Context Help window. You can use all other commented lines for internal documentation. - Define the executable body of the function for
*script*.

The following example of a user-defined function returns both the sum and the difference of two input values:

function [sum, difference] = sumdifference(a, b)

%Finds the sum and the difference of a and b

sum = a + b;

difference = a - b;

For this example, you must save the function as sumdifference.m. The filename for a function must be the same as the name of the function and must have a lowercase .m extension. You can use the following syntax to call sumdifference.m from the **LabVIEW MathScript Window** or a MathScript Node:

[sum, difference] = sumdifference(5, 9)

The following examples show the valid function signatures for foo, where foo represents a user-defined function:

function foo | function a = foo | function [a b] = foo |

function foo() | function a = foo() | function [a b] = foo() |

function foo(g) | function a = foo(g) | function [a b] = foo(g) |

function foo(g, h) | function a = foo(g, h) | function [a b] = foo(g, h) |

If you define multiple functions in one MathScript file, all functions following the first are subfunctions and are accessible only to the main function. You cannot call functions recursively. For example, foo cannot call foo. LabVIEW also does not allow circular recursive function calls. For example, foo cannot call bar if bar calls foo.

The following is an example of a MathScript file that uses proper syntax. fadd3 is the main function, and add2 is a subfunction of fadd3.

function a = fadd3(x)

% fadd3 adds 3 to the input value x.

a = 1 + add2(x);

function a = add2(x)

% add2 is a subfunction. Only fadd3 can call this function.

% add2 computes the constant 2 using Euler's formula:

% e^(i*theta) = cos(theta) + i*sin(theta)

a = x - (exp(i*pi) - 1);

You can specify optional inputs and outputs for a function. Use the arginnum function to determine the number of input arguments supplied to the function that calls arginnum. If the number of inputs supplied is less than the maximum number of inputs for the function, you can assign default values to define the optional inputs. Use the argoutnum function to determine the number of output arguments requested from the function that calls argoutnum. If the number of outputs requested is less than the maximum number of outputs for the function, you can bypass the calculation of the unrequested outputs.

Helpful

Not Helpful