Programs->R2->Functions
Functions
Home
  What's New
  Downloads
Programs
  CSS Editor
  TCP/IP
  rInstaller
  R
  R2
    Functions
    How It Works
    Basic Types
    Reference
  Java
Publications
  ODBC Browser
  Internet Server

Red Corona
Mail me




Note: these pages use the APL2741 or APLSans font, both available from Causeway.

Functions in R2

There are two ways to define functions in R2: using the editor (no syntax colouring I'm afraid), or direct definition (xx„{¹„¸+4×¾}). A function defined using direct definition can then be edited normally; direct definition in this way is mainly a shorthand, although it does allow dynamic function style coding (functions defined as part of the execution). A function will echo to the session anything left on the stack at the end of a line; to return a result, assign to the special variable ¹.

Functions are niladic, monadic or dyadic depending on the symbols present in it. If there is an occurence of ¸ it is assumed to be dyadic, if there is only a ¾ then it is monadic and if neither, it is niladic. A sample function could be:
© Make two things the same shape
¾„(½¸)½¾
© Put them back
¹„¸ ¾

Localisation and Scope

There is currently no such thing as a local function; any function defined within another function using direct definition will still exist outside it. However, any variable declared within a function is local to it, and will not exist beyond the function and any functions it calls. Even if a variable of the same name exists outside it, it cannot be changed (although it can be read). This is demonstrated in the following example.

fn is defined as:
var
var„6
var
. The following is run at the session:
var„"abc"
fn
     abc
The original value of var
     6
The local value of var as defined in fn
var
     abc
The original value of var has been restored

To escape this scope trap, you need to specify that a variable is to be globalised. This is done by prefixing the name with one of the characters in ŒGLOBALS (usually or ).


Page and site © Richard Smith 2001
Please send any comments to richard@redcorona.com - thank you.
 Top   Home