REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 26-Jan-2010 Edit History  

REBOL 3 Concepts: Tour: Functions

Pending Revision

This document was written for R2 and has yet to be revised for R3.


This section is obsolete and being replaced. It is kept only as a source for new content.

A function is a block with variables that are given new values each time the block is evaluated. These variables are called the arguments of the function.

In the following example, the word sum is set to refer to a function that accepts two arguments, a and b :

sum: func [a b] [a + b]

In the above example, func is used to define a new function. The first block in the function describes the arguments of the function. The second block is the block of code that gets evaluated when the function is used. In this example, the second block adds two values and returns the result.

The next example illustrates one use of the function sum that was defined in the previous example:

print sum 2 3

Some functions need local variables as well as arguments. To define this type of function, use function!, instead of func, as shown in the following example:

average: function [series] [total] [
    total: 0
    foreach value series [total: total + value]
    total / (length? series)

print average [37 1 42 108]

In the above example, the word series is an argument and the word total is a local variable used by the function for calculation purposes.

The function argument block can contain strings to describe the purpose of a function and its argument, as shown in the following example:

average: function [
    "Return the numerical average of numbers"
    series "Numbers to average"
] [total] [
    total: 0
    foreach value series [total: total + value]
    total / (length? series)

These descriptive strings are kept with the function and can be viewed by asking for help about the function, as shown below:

help average
    AVERAGE series
    Return the numerical average of numbers
    AVERAGE is a function value.
    series -- Numbers to average (Type: any)

  TOC < Back Next > - WIP Wiki Feedback Admin