I have a simple function with two variables as
/func {
/var1 exch def
/var2 exch def
... process ...
} def
(var2)(var1)func
I want to make var2 optional. However, if not providing var2, it results in stackunderflow error. How can I make a if statement to catch var2 only if the stack is not empty, and probably assign a default value if the stack is empty.
Something like
(Stack is no empty) {/var2 exch def}{/var2 (default) def} ifelse
The
count
operator counts the number of operands on the stack. You might like to instead use[
to put a mark on the stack and then usecounttomark
instead. This saves you getting confused by operands being left over, or not yet used, when your routine is called from other routines. Of course it means you have to supply the[
as an operand on the stack.The other usual method is to have the top operand be an integer counting the number of additional operands..