|REBOL 3 Docs||Guide||Concepts||Functions||Datatypes||Errors|
|TOC < Back Next >||Updated: 17-Aug-2010 Edit History|
Defines a user function with given spec and body.
spec [block!] - Help string (opt) followed by arg words (and opt type and string)
body [block!] - The body block of the function
The func function creates new functions from a spec block and a body block.
name: func [spec] [body]
The spec block specifies the interface to the function. It can begin with an optional title string which used by the help function. That is followed by words that specify the arguments to the function. Each of argument can include an optional block of datatypes to specify the valid datatypes for the argument. Each may be followed by a comment string which describes the argument in more detail.
The argument words may also specify a few variations on the way the argument will be evaluated. The most common is 'word which indicates that a word is expected that should not be evaluated (the function wants its name, not its value). A :word may also be given which will get the value of the argument, but not perform full evaluation.
To add refinements to a function supply a slash (/) in front of an argument's word. Within the function the refinement can be tested to determine if the refinement was present. If a refinement is followed by more arguments, they will be associated with that refinement and are only evaluated when the refinement is present.
Local variables are specified after a /local refinement.
sum: func [a b] [a + b] print sum 123 321 444
sum: func [nums [block!] /average /local total] [ total: 0 foreach num nums [total: total + num] either average [total / (length? nums)][total] ] print sum [123 321 456 800] print sum/average [123 321 456 800] 425
print-word: func ['word] [print form word] print-word testing testing
|TOC < Back Next >||REBOL.com - WIP Wiki||Feedback Admin|