Question: Handling optional inputs to procedures?

How should optional input parameters be handled for procedures. The example has three optional inputs 

vars:=[x,y]   ,  clr:="b"   and prnt:="y"  . if one wants to change prnt to"n", vars and clr values must be entered.

{vars:=[x,y]  } , { clr:="b" }  and{ prnt:="y"}  this is a good method because one just enteres prnt="n".  But have to remember the input parameter name prnt.

I have a 3rd option, but it is to complicated and probably unreliable to use in practice.  and with more than 3 optional inputs too difficult to code.

I am wondering  are the other approaches and what is the prefered methodology. I have about 30+ procedures to apply this to in a package.

Edit:-  I can change the prnt to boolean true, false instead of "y" , "n".  That would make the all the optional inputs different types.

In Test1 its should be vars::list:=[x,y] not vars::{list , `string`}:=[x,y]

restart

Geomclr := "b"

"b"

(1)

NULL

NULL

NULL

Test1 := proc (A, B, vars::{list, string} := [x, y], clr::string := Geomclr, prnt::string := "y") if vars::string then clr := vars; vars := [x, y] end if; if clr = "y" or clr = "n" then prnt := clr; clr := Geomclr end if; print(clr); if prnt = "y" then print("good") end if; A*vars[1]+B*vars[2] end proc

NULL

Test1(A, B)

A*x+B*y

(2)

Test1(A, B, [r, s], "n")

Error, (in Test1) invalid left hand side in assignment

 

Test1(A, B, "g")

Error, (in Test1) invalid left hand side in assignment

 

Test1(A, B, "n")

Error, (in Test1) invalid left hand side in assignment

 

Test1(A, B, [x, y], "b", "n")

A*x+B*y

(3)

 

 

NULL

``

NULL

 

NULL

Test2 := proc (A, B, { vars::{list} := [x, y], clr::string := Geomclr, prnt::string := "y" }) print(clr); if prnt = "y" then print("good") end if; A*vars[1]+A*vars[2] end proc

NULL

NULL

Test2(A, B)

A*x+A*y

(4)

Test2(A, B, clr = "r")

A*x+A*y

(5)

Test2(A, B, prnt = "n")

A*x+A*y

(6)

Test2(A, B, prnt = "n")

A*x+A*y

(7)

Test2(A, B, prnt = "n", clr = "green", l = [r, s])

A*x+A*y

(8)

``

 

# 3 This is possible but is a very complicated method of handling the optional inputs and difficult the handle altered sequence on inputs.

NULL

Test3 := proc (A, B, vars::{list, string} := [x, y], clr::string := Geomclr, prnt::string := "y") local varsl, clrl, prntl; global Geomclr; varsl := vars; clrl := clr; prntl := prnt; if vars::string then varsl := [x, y]; if vars = "y" or vars = "n" then prntl := vars; clrl := Geomclr elif vars = "r" or vars = "g" or vars = "b" then clrl := vars end if elif vars::list and clr = "y" or clr = "n" then prntl := clr; clrl := Geomclr end if; if clr = "y" or clr = "n" and vars::string then prntl := clr; clrl := vars end if; print("Colour print out ", clrl); if prntl = "y" then print("This is a test Message") end if; A*varsl[1]+B*varsl[2] end proc

NULL

Test3(A, B)

A*x+B*y

(9)

Test3(A, B, [r, s])

A*r+B*s

(10)

Test3(A, B, [r, s], "n")

A*r+B*s

(11)

Test3(A, B, "n")

A*x+B*y

(12)

Test3(A, B, [r, s], "r", "n")

A*r+B*s

(13)

Test3(A, B, "r", "n")

A*x+B*y

(14)

Test3(A, B, "n", "r")

A*x+B*y

(15)

NULL


 

Download Q_2024-02-25_Test_proc_Args.mw

Please Wait...