exprv4:how-to:use_variables

How To Use Variables

A variable is a name for a value. Use variables when you want to store a number, text, result, setting, or object and use it later.

Variables make scripts easier to read because names explain what the values mean.

feed = 800;
depth = -2;
safe_z = 5;

Store A Value

Use `=` to store a value in a variable.

a = 10;
name = 'Tool 1';
diameter = 6;

After a value is stored, use the variable name in later expressions.

diameter = 6;
radius = diameter / 2;
radius;

The final result is `3`.


Change A Value

Assign a new value to the same variable.

feed = 800;
feed = 1000;
feed;

The final result is `1000`.

This is useful when a script starts with a default value and changes it later.

safe_z = 5;
 
tool_length = 60;
 
if(tool_length > 50)
{
    safe_z = 10;
};

Calculate From Variables

Variables can be used inside calculations.

width = 40;
height = 25;
area = width * height;
area;

The final result is `1000`.

For machine values, this is often clearer than one long expression.

tool_diameter = 6;
tool_radius = tool_diameter / 2;
stepover = tool_radius * 0.4;
stepover;

Use Variables In MDI

In MDI, start input with `=` to evaluate Expr instead of G-code.

=diameter=6;
radius=diameter/2;
radius;

This outputs:

3

If the host uses the same session for later MDI input, variables can remain available for the next command. If the host uses a temporary session, they exist only for that evaluation.


Use `print(…)` when you want to show values while a script runs.

tool = 3;
diameter = 6;
 
print('tool ', tool);
print('diameter ', diameter);

You can also build a text result with +:

'Tool ' + tool + ', diameter ' + diameter;

For debugging scripts, `print(…)` is usually clearer because it can show several lines while the script continues.


Use Clear Names

Prefer names that describe the value.

Good:

tool_diameter = 6;
probe_feed = 100;
safe_z = 5;

Harder to read:

d = 6;
f = 100;
z = 5;

Short names such as `x`, `y`, `z`, `i`, and `n` are fine for small calculations, coordinates, and counters. For settings and results, descriptive names are better.


Unknown Variables Return none()

If you read a variable that has not been assigned, Expr returns `none()`.

missing_value;

`none()` means there is no value. It is not the same as `0` or an empty string.

You can check for it:

if(missing_value.is_none())
{
    print('missing value');
};

This can be useful, but it can also hide spelling mistakes. If a value should exist, check the variable name carefully.


Use set For Temporary Local Values

Top-level variables are created with normal assignment.

feed = 800;
safe_z = 5;

Use `set` when you want a temporary local variable inside a block, function, or method.

feed = 800;
 
if(feed > 500)
{
    set message = 'fast feed';
    print(message);
};
 
message;    // none()

Here, `message` exists only inside the `if` block.

`set` is not valid at top level. Use it only inside a block, function, or method when you want a local temporary value.


A few variables are easy to manage:

safe_z = 5;
feed = 100;
depth = -2;

When many values belong to the same thing, consider using a class object instead.

class ProbeJob(feed)
{
    safe_z = 5;
    probe_feed = feed;
    depth = -2;
}
 
probe = ProbeJob(120);

This keeps related values under one name: `probe`.


Common Mistakes

Forgetting That Assignment Changes The Value

a = 10;
a = 20;
a;

The final value is `20`.

Misspelling A Variable Name

probe_feed = 100;
probe_fed;

`probe_fed` is a different name, so it returns `none()`.

Using A Variable Before Setting It

radius = diameter / 2;   // error if diameter is none()

Set values before using them in calculations.

diameter = 6;
radius = diameter / 2;

Try This

In MDI, try:

=width=40;
height=25;
area=width*height;
print('area ', area);
area;

The final result is `1000`.


See Also

exprv4/how-to/use_variables.txt · Last modified: by 127.0.0.1

Page Tools