r/openscad • • 2d ago

variable name as part of parameters in module call (..., varname1=n, varname2=n)

Hello,
Excuse my perhaps stupid question.
I have the following statement: module_abc(20, varname1=4, varname2=13);
What actual means the listing of (a variable name) as part of the parameter with the assignment of a value ?
e.g. varname1= and varname2=

And what are the implication I have to be aware of?
Is there perhaps somewhere a piece of documentation about it that I have missed?
Thanks for answering.

0 Upvotes

5 comments sorted by

4

u/hymie0 2d ago

Specifying the parameter name overrides the order-based default parameters.

For example, if a function takes six parameters, and the default values are good for all but the fifth, you can specify functionname(param5=12) without having to specify all six parameter values.

4

u/DontGetMeStarted2025 2d ago
module foo1(x,y,z) // takes three parameters, no default values available
module foo2(x,y,z=5) // takes 2 or 3 parameters, z can be omitted and will be assigned 5

foo1(3,2,1) // inside foo1, x=3, y=2 and z=1
foo1(3,2) // inside foo1, x=3, y=2 and z=undefined (mostly an error)
foo2(3,2) // inside foo2, x=3, y=2 and z=5

foo1(z=3,y=2,x=1) // call foo1, but specify the values "out of order"
foo2(y=2,x=1) // call foo2, but specify the values "out of order", z will be 5

1

u/gasstation-no-pumps 2d ago

Your example is faulty as the module definition needs to have a variable name for each parameter (20 is a constant, not a variable name). The "=<expression>" after a parameter name gives a default value for that parameter, so that the parameter may be omitted in a call and assigned the default value.

I generally provide default values for all parameters when defining modules.

1

u/Dependent-Bridge-740 1d ago

I'm hoping at least my question came across properly.

1

u/oldesole1 1d ago

When calling a module, you may assign values to specific parameters by specifying the parameter name to the left of the =.

Your example module_abc(20, varname1=4, varname2=13); means that the first parameter is set to 20, parameter with name varname1 is set to 4, and parameter with name varname2 is set to 13.

If you assign a parameter by name that is already handled through positional assignment, then it should throw an error in the console. That is, if the first parameter in the module definition for module_abc is named varname1, it should throw an error because it has already been assigned to 20.

Parameter name assignment does not affect variables in the same scope:

varname1 = 15;

module_abc(20, varname1=4, varname2=13);

echo(varname1);

The console will show 15.

I believe all of the above applies to function parameters as well.

Also, modules and functions can access variables defined in all parent scopes, without requiring that they are passed as parameters.

This can be useful to quickly write code, but can also bite you if things get complicated. So use discretion.

More details can be read here:

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Modules