### Definition
proc myproc {alpha {beta {}} {gamma green}} {
puts [list $alpha $beta $gamma]
}
### Use
myproc A
# => A {} green
myproc A B
# => A B green
myproc A B C
# => A B C
This procedure accepts one, two, or three arguments: those parameters whose names are the first item in a two-item list are optional. The parameter variables (alpha
, beta
, gamma
) get as many argument values as are available, assigned from left to right. Parameter variables that don't get any argument values instead get their values from the second item in the list they were a part of.
Note that optional arguments must come at the end of the argument list. If argumentN-1 is optional, argumentN must be optional too. If in a case, where user have argumentN but not argumentN-1, default value of argumentN-1 needs to be explicitly mentioned before argumentN, while calling the procedure.
myproc A B C D
# (ERROR) wrong # args: should be "myproc alpha ?beta? ?gamma?"
The procedure does not accept more than three arguments: note that a helpful error message describing the argument syntax is automatically created.