question: calling functions without passing parameters
[1/5] from: jeff-patton:home at: 8-Jan-2001 20:32
Thanks to you, Ingo & Andrew for your responses. Three different, good
approaches!
Cheers,
-Jeff
[2/5] from: jeff-patton:home at: 7-Jan-2001 11:45
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
[3/5] from: lmecir:mbox:vol:cz at: 7-Jan-2001 21:23
This is the current way:
f: func [n [number! unset!]] [
if not value? 'n [n: 0]
print n
]
[4/5] 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?
<<quoted lines omitted: 4>>
> [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(_)| (_|
._| ._|
[5/5] from: al:bri:xtra at: 8-Jan-2001 15:38
> 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?
F: func [/Option Value] [
all [
none? Option
Value: "default value"
]
print Value
]
F
F/Option 123
Andrew Martin
ICQ: 26227169 http://members.nbci.com/AndrewMartin/
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted