function {base}R Documentation

Function Definition


These functions provide the base mechanisms for defining new functions in the R language.


function( arglist ) expr


arglist Empty or one or more name or name=expression terms.
value An expression.


The names in an argument list can be back-quoted non-standard names (see ‘backquote’).

If value is missing, NULL is returned. If it is a single expression, the value of the evaluated expression is returned.

If the end of a function is reached without calling return, the value of the last evaluated expression is returned.


Prior to R 1.8.0, value could be a series of non-empty expressions separated by commas. In that case the value returned is a list of the evaluated expressions, with names set to the expressions where these are the names of R objects. That is, a=foo() names the list component a and gives it the value which results from evaluating foo().

This has been deprecated (and a warning is given), as it was never documented in S, and whether or not the list is named differs by S versions.


Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also

args and body for accessing the arguments and body of a function.

debug for debugging; using invisible inside return(.) for returning invisibly.


norm <- function(x) sqrt(x%*%x)

## An anonymous function:
(function(x,y){ z <- x^2 + y^2; x+y+z })(0:7, 1)

[Package base version 2.5.0 Index]