[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  function: Function Definition

##  Description

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

##  Usage

    
    
    function( arglist ) expr
    return(value)

##  Arguments

arglist

Empty or one or more name or name=expression terms.

expr

An expression.

value

An expression.

##  Technical details

This type of function is not the only type in  R  : they are called _closures_
(a name with origins in LISP) to distinguish them from [ primitive
](/link/primitive?package=base&version=3.6.2) functions.

A closure has three components, its ` [ formals
](/link/formals?package=base&version=3.6.2) ` (its argument list), its ` [
body ](/link/body?package=base&version=3.6.2) ` ( ` expr ` in the ‘Usage’
section) and its ` [ environment
](/link/environment?package=base&version=3.6.2) ` which provides the enclosure
of the evaluation frame when the closure is used.

There is an optional further component if the closure has been byte-compiled.
This is not normally user-visible, but is indicated when functions are
printed.

##  Details

The names in an argument list can be back-quoted non-standard names (see ‘ [
backquote ](/link/backquote?package=base&version=3.6.2) ’).

If ` value ` is missing, ` NULL ` is returned. If it is a single expression,
the value of the evaluated expression is returned. (The expression is
evaluated as soon as ` return ` is called, in the evaluation frame of the
function and before any ` [ on.exit
](/link/on.exit?package=base&version=3.6.2) ` expression is evaluated.)

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

##  References

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

##  See Also

` [ args ](/link/args?package=base&version=3.6.2) ` .

` [ formals ](/link/formals?package=base&version=3.6.2) ` , ` [ body
](/link/body?package=base&version=3.6.2) ` and ` [ environment
](/link/environment?package=base&version=3.6.2) ` for accessing the component
parts of a function.

` [ debug ](/link/debug?package=base&version=3.6.2) ` for debugging; using ` [
invisible ](/link/invisible?package=base&version=3.6.2) ` inside ` return(.) `
for returning _invisibly_ .

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Ffunction&utm_source=r-docs&utm_medium=docs&utm_term=function&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    norm <- function(x) sqrt(x%*%x)
    norm(1:4)
    
    ## An anonymous function:
    (function(x, y){ z <- x^2 + y^2; x+y+z })(0:7, 1)
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Ffunction&utm_source=r-docs&utm_medium=docs&utm_term=function&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)