discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

passiing argument(s) to a module?

GH
Gene Heskett
Thu, Jul 15, 2021 8:20 AM

Greetings all;

Scope of arguments seems to be a problem in my current code for this
drive, and I find a need to pass a two values to an oft used module
called keys1,2,3 at this time. but if I could pass two arguments in the
call by name, not even using the same name but the value I could strip 2
and 3 back out of it.

What does that syntax look like?

Thanks all.

Cheers, Gene Heskett

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
If we desire respect for the law, we must first make the law respectable.

Greetings all; Scope of arguments seems to be a problem in my current code for this drive, and I find a need to pass a two values to an oft used module called keys1,2,3 at this time. but if I could pass two arguments in the call by name, not even using the same name but the value I could strip 2 and 3 back out of it. What does that syntax look like? Thanks all. Cheers, Gene Heskett -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) If we desire respect for the law, we must first make the law respectable. - Louis D. Brandeis Genes Web page <http://geneslinuxbox.net:6309/gene>
KT
Kevin Toppenberg
Thu, Jul 15, 2021 8:57 AM

Gene,

I'm not sure I am following your difficulty.    What you are wanting seems
like exactly how it was designed.
I found this documentation:
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Modules

Example:

---=========================

x=5;
y=12;
z = 3;

MyFancyShape(x, y, z);  //example of use #1

translate([40,40,40])
MyFancyShape(HowLong = 7, HowWide=2, HowHigh = 20);  //Example of use #2

translate([-40,-40,-40])
MyFancyShape(HowLong = x, HowWide=y, HowHigh = z);  //Example of use #3

module MyFancyShape(HowLong = 20, HowWide = 10, HowHigh = 30) {
cube([HowLong, HowWide, HowHigh]);
}

---=========================

The above module MyFancyShape has 3 defined parameters: HowLong, HowWide,
HowHigh.  When calling the parameters, one can just supply them in order
(first example), or by name (example #2).  Example #3 just shows that one
can use variable names and parameter names together.

Does that help?

Best wishes,
Kevin T

On Thu, Jul 15, 2021 at 4:21 AM Gene Heskett gheskett@shentel.net wrote:

Greetings all;

Scope of arguments seems to be a problem in my current code for this
drive, and I find a need to pass a two values to an oft used module
called keys1,2,3 at this time. but if I could pass two arguments in the
call by name, not even using the same name but the value I could strip 2
and 3 back out of it.

What does that syntax look like?

Thanks all.

Cheers, Gene Heskett

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
If we desire respect for the law, we must first make the law respectable.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Gene, I'm not sure I am following your difficulty. What you are wanting seems like exactly how it was designed. I found this documentation: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Modules Example: ========================================================== x=5; y=12; z = 3; MyFancyShape(x, y, z); //example of use #1 translate([40,40,40]) MyFancyShape(HowLong = 7, HowWide=2, HowHigh = 20); //Example of use #2 translate([-40,-40,-40]) MyFancyShape(HowLong = x, HowWide=y, HowHigh = z); //Example of use #3 module MyFancyShape(HowLong = 20, HowWide = 10, HowHigh = 30) { cube([HowLong, HowWide, HowHigh]); } ========================================================== The above module MyFancyShape has 3 defined parameters: HowLong, HowWide, HowHigh. When calling the parameters, one can just supply them in order (first example), or by name (example #2). Example #3 just shows that one can use variable names and parameter names together. Does that help? Best wishes, Kevin T On Thu, Jul 15, 2021 at 4:21 AM Gene Heskett <gheskett@shentel.net> wrote: > Greetings all; > > Scope of arguments seems to be a problem in my current code for this > drive, and I find a need to pass a two values to an oft used module > called keys1,2,3 at this time. but if I could pass two arguments in the > call by name, not even using the same name but the value I could strip 2 > and 3 back out of it. > > What does that syntax look like? > > Thanks all. > > Cheers, Gene Heskett > -- > "There are four boxes to be used in defense of liberty: > soap, ballot, jury, and ammo. Please use in that order." > -Ed Howdershelt (Author) > If we desire respect for the law, we must first make the law respectable. > - Louis D. Brandeis > Genes Web page <http://geneslinuxbox.net:6309/gene> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
GC
Gareth Chen
Thu, Jul 15, 2021 9:34 AM

I think maybe Gene is interested in using default values to call the same
module with different numbers of arguments? In that case, I believe you can
supply arguments without using their name if you list them in order, and
any later arguments use their default values. If you want to use a later
argument without the ones that come before it you can do that by specifying
the name. Using the same MyFancyShape:

module MyFancyShape(HowLong = 20, HowWide = 10, HowHigh = 30) {
cube([HowLong, HowWide, HowHigh]);
}

x=5;
y=12;
z = 3;

// These are equivalent
MyFancyShape(x);
MyFancyShape (x, 10, 30);

// These are equivalent
MyFancyShape(x, y);
MyFancyShape (x, y, 30);

// These are equivalent
MyFancyShape (x, HowHigh=z);
MyFancyShape (x, 10, z);

// If you use the names you can put them in any order. These are equivalent
MyFancyShape (HowWide=y, HowHigh=z, HowLong=x);
MyFancyShape (x, y, z);

I think maybe Gene is interested in using default values to call the same module with different numbers of arguments? In that case, I believe you can supply arguments without using their name if you list them in order, and any later arguments use their default values. If you want to use a later argument without the ones that come before it you can do that by specifying the name. Using the same MyFancyShape: module MyFancyShape(HowLong = 20, HowWide = 10, HowHigh = 30) { cube([HowLong, HowWide, HowHigh]); } x=5; y=12; z = 3; // These are equivalent MyFancyShape(x); MyFancyShape (x, 10, 30); // These are equivalent MyFancyShape(x, y); MyFancyShape (x, y, 30); // These are equivalent MyFancyShape (x, HowHigh=z); MyFancyShape (x, 10, z); // If you use the names you can put them in any order. These are equivalent MyFancyShape (HowWide=y, HowHigh=z, HowLong=x); MyFancyShape (x, y, z);
GH
Gene Heskett
Thu, Jul 15, 2021 11:41 AM

On Thursday 15 July 2021 04:57:24 Kevin Toppenberg wrote:

Gene,

I'm not sure I am following your difficulty.    What you are wanting
seems like exactly how it was designed.
I found this documentation:
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functi
ons_and_Modules#Modules

Example:

---=========================

x=5;
y=12;
z = 3;

MyFancyShape(x, y, z);  //example of use #1

translate([40,40,40])
MyFancyShape(HowLong = 7, HowWide=2, HowHigh = 20);  //Example of
use #2

translate([-40,-40,-40])
MyFancyShape(HowLong = x, HowWide=y, HowHigh = z);  //Example of use
#3

module MyFancyShape(HowLong = 20, HowWide = 10, HowHigh = 30) {
cube([HowLong, HowWide, HowHigh]);
}

---=========================

The above module MyFancyShape has 3 defined parameters: HowLong,
HowWide, HowHigh.  When calling the parameters, one can just supply
them in order (first example), or by name (example #2).  Example #3
just shows that one can use variable names and parameter names
together.

Does that help?

Best wishes,
Kevin T

On Thu, Jul 15, 2021 at 4:21 AM Gene Heskett gheskett@shentel.net

wrote:

Greetings all;

Scope of arguments seems to be a problem in my current code for this
drive, and I find a need to pass a two values to an oft used module
called keys1,2,3 at this time. but if I could pass two arguments in
the call by name, not even using the same name but the value I could
strip 2 and 3 back out of it.

What does that syntax look like?

Yes I think so, what I had in mind would be to define:

module keys(h=y,d=z,t)
{
if key(key != 0) translate([0,0,t]) keys(y, z);// make it optional
};

then later:
mode=1; // mode value determining which module to build
key=1;

if(mode=2)
{
//calls to other modules that use keys.
};

See attached snapshot, designed for easier assembly but no rotational
slippage allowed, either of this part within the housing, or the next
part contained by the smaller knobs on the inside face of the pictured
part. And I need to scale so the outer cuts here are varname+.2mm, while
the bumps in the housing inner wall these cuts engage are sized at
varname-.2, leaving room for a press fit. The printer expands about half
the nozzle size, so I need to expand the cuts, and shrink the matching
knobs an equal amount for a fit that actually works.

Many thanks to both replies to my post, now I know I /can/ make it work.

Thanks all.

Cheers, Gene Heskett

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
If we desire respect for the law, we must first make the law
respectable. - Louis D. Brandeis
Genes Web page http://geneslinuxbox.net:6309/gene


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Cheers, Gene Heskett

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
If we desire respect for the law, we must first make the law respectable.

On Thursday 15 July 2021 04:57:24 Kevin Toppenberg wrote: > Gene, > > I'm not sure I am following your difficulty. What you are wanting > seems like exactly how it was designed. > I found this documentation: > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functi >ons_and_Modules#Modules > > Example: > ========================================================== > > x=5; > y=12; > z = 3; > > MyFancyShape(x, y, z); //example of use #1 > > translate([40,40,40]) > MyFancyShape(HowLong = 7, HowWide=2, HowHigh = 20); //Example of > use #2 > > translate([-40,-40,-40]) > MyFancyShape(HowLong = x, HowWide=y, HowHigh = z); //Example of use > #3 > > > module MyFancyShape(HowLong = 20, HowWide = 10, HowHigh = 30) { > cube([HowLong, HowWide, HowHigh]); > } > > ========================================================== > > The above module MyFancyShape has 3 defined parameters: HowLong, > HowWide, HowHigh. When calling the parameters, one can just supply > them in order (first example), or by name (example #2). Example #3 > just shows that one can use variable names and parameter names > together. > > Does that help? > > Best wishes, > Kevin T > > On Thu, Jul 15, 2021 at 4:21 AM Gene Heskett <gheskett@shentel.net> wrote: > > Greetings all; > > > > Scope of arguments seems to be a problem in my current code for this > > drive, and I find a need to pass a two values to an oft used module > > called keys1,2,3 at this time. but if I could pass two arguments in > > the call by name, not even using the same name but the value I could > > strip 2 and 3 back out of it. > > > > What does that syntax look like? > > Yes I think so, what I had in mind would be to define: module keys(h=y,d=z,t) { if key(key != 0) translate([0,0,t]) keys(y, z);// make it optional }; then later: mode=1; // mode value determining which module to build key=1; if(mode=2) { //calls to other modules that use keys. }; See attached snapshot, designed for easier assembly but no rotational slippage allowed, either of this part within the housing, or the next part contained by the smaller knobs on the inside face of the pictured part. And I need to scale so the outer cuts here are varname+.2mm, while the bumps in the housing inner wall these cuts engage are sized at varname-.2, leaving room for a press fit. The printer expands about half the nozzle size, so I need to expand the cuts, and shrink the matching knobs an equal amount for a fit that actually works. Many thanks to both replies to my post, now I know I /can/ make it work. > > Thanks all. > > > > Cheers, Gene Heskett > > -- > > "There are four boxes to be used in defense of liberty: > > soap, ballot, jury, and ammo. Please use in that order." > > -Ed Howdershelt (Author) > > If we desire respect for the law, we must first make the law > > respectable. - Louis D. Brandeis > > Genes Web page <http://geneslinuxbox.net:6309/gene> > > _______________________________________________ > > OpenSCAD mailing list > > To unsubscribe send an email to discuss-leave@lists.openscad.org Cheers, Gene Heskett -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) If we desire respect for the law, we must first make the law respectable. - Louis D. Brandeis Genes Web page <http://geneslinuxbox.net:6309/gene>
GC
Gareth Chen
Thu, Jul 15, 2021 1:13 PM

module keys(h=y,d=z,t)

The convention is usually to put arguments without defaults first so that
you can use them without specifying the argument name.

>> module keys(h=y,d=z,t) The convention is usually to put arguments without defaults first so that you can use them without specifying the argument name.