[REBOL] Re: question: calling functions without passing parameters
From: ingo::2b1::de at: 7-Jan-2001 21:53
Hi Jeff,
yes, you can:
>> f: func [a [any-type!]][
either value? 'a [print ["f: " a]][print "f: No a given"]
]
>> f 7
f: 7
>> f
f: No a given
But there's a little drawback, should you have the following
two lines in a script (or >> f x: 7 ; on the rebol shell)
f
x: 7
(Meaning, f with no arguments, followed by a sequence to set
x to the value seven)
You'll end up with
f: 7
because 'a will happily eat the set-word, and get's the value
seven, you'd have to be carefull, that f with no arguments is
always at the end of a block, or is enclosed be braces
>> (f) x: 7
f: No a given
== 7
So, the best way to do what you want is to use paths, like this
>> f: func [/a value][either a [print ["f: " value]][print "f: No value given"]]
>> f x: 7
f: No value given
== 7
>> f 7
f: No value given
== 7
>> f/a 7
f: 7
I hope that helps,
Ingo
Once upon a time Jeff Patton spoketh thus:
> I'm new to Rebol, but looking for a quick answer I can't find in the core manual.
>
> Can I specify a function with an optional parameter? - one that may or may not be passed,
or that could have a default value if not passed?
>
> thanks in advance,
>
> -Jeff
>
> --
> To unsubscribe from this list, please send an email to
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
>
--
_ . _
ingo@)|_ /| _| _ <We ARE all ONE www._|_o _ _ ._ _
www./_|_) |o(_|(/_ We ARE all FREE> ingo@| |(_|o(_)| (_|
._| ._|