Rebol Programming/Design Guide/Take advantage of return values

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Many functions return useful values.

For instance, the INSERT function returns the series just past the insertion.

IF, EITHER, and most of the looping functions return the value of the block they evaluated.

>> j: 0
== 0
>> print foreach entry [red green blue water aqua] [j: j + 1]
5

Don't use RETURN in every function you write[edit | edit source]

The last value evaluated is returned automatically in user-defined functions. While you can use the RETURN function to exit a function early and return a value, you do not need to use it in every function you write. One reason not to do so is that it is another function call, with the overhead that entails. Another is that using RETURN only for early exit scenarios (as you would use EXIT) makes those special cases more obvious.

>> fn: does [100]
>> fn
== 100
>> fn: does [return 100]
>> fn
== 100