r/openscad • u/Dependent-Bridge-740 • 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
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
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/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 to20, parameter with namevarname1is set to4, and parameter with namevarname2is set to13.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_abcis namedvarname1, it should throw an error because it has already been assigned to20.Parameter name assignment does not affect variables in the same scope:
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