Writing your own functions

  As we have seen informally along the way, the . language allows the user to create objects of mode function. These are true . functions that are stored in a special internal form and may be used in further expressions and so on. In the process the language gains enormously in power, convenience and elegance, and learning to write useful functions is one of the main ways to make your use of . comfortable and productive.

It should be emphasized that most of the functions supplied as part of the . system, such as mean(), var(), postscript() and so on, are themselves written in . and thus do not differ materially from user written functions.

A function is defined by an assignment of the form

name <- function(arg1 , arg2 , ...) expression

The expression is an . expression, (usually a grouped expression), that uses the arguments, argi , to calculate a value. The value of the expression is the value returned for the function.

A call to the function then usually takes the form name(expr1 , expr2 , ...) and may occur anywhere a function call is legitimate.



 

Jeff Banfield
2/13/1998