The R Project SVN R

Rev

Rev 85234 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/function.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
85061 smeyer 3
% Copyright 1995-2023 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
2 r 6
\name{function}
58825 ripley 7
\alias{function}
8
\alias{return}
9
\alias{closure}
10
 
2 r 11
\title{Function Definition}
12
\usage{
54642 hornik 13
\special{function( arglist ) expr}
79553 luke 14
\special{\\( arglist ) expr}
54642 hornik 15
\special{return(value)}
2 r 16
}
17
\description{
24330 ripley 18
  These functions provide the base mechanisms for defining
19
  new functions in the \R language.
2 r 20
}
6994 pd 21
\arguments{
85061 smeyer 22
  \item{arglist}{empty or one or more (comma-separated) \samp{name} or
85234 smeyer 23
    \samp{name = expression} terms
85061 smeyer 24
    and/or the special token \code{\link{...}}.}
25
  \item{expr}{an expression.}
26
  \item{value}{an expression.}
6994 pd 27
}
28
\details{
40456 ripley 29
  The names in an argument list can be back-quoted non-standard names
30
  (see \sQuote{\link{backquote}}).
6994 pd 31
 
24330 ripley 32
  If \code{value} is missing, \code{NULL} is returned.  If it is a
33
  single expression, the value of the evaluated expression is returned.
46383 ripley 34
  (The expression is evaluated as soon as \code{return} is called, in
35
  the evaluation frame of the function and before any
36
  \code{\link{on.exit}} expression is evaluated.)
61433 ripley 37
 
24330 ripley 38
  If the end of a function is reached without calling \code{return}, the
39
  value of the last evaluated expression is returned.
79553 luke 40
 
41
  The shorthand form \code{\\(x) x + 1} is parsed as \code{function(x) x
42
    + 1}. It may be helpful in making code containing simple function
43
  expressions more readable.
24330 ripley 44
}
58825 ripley 45
\section{Technical details}{
46
  This type of function is not the only type in \R: they are called
47
  \emph{closures} (a name with origins in LISP) to distinguish them from
48
  \link{primitive} functions.
49
 
50
  A closure has three components, its \code{\link{formals}} (its argument
51
  list), its \code{\link{body}} (\code{expr} in the \sQuote{Usage}
52
  section) and its \code{\link{environment}} which provides the
53
  enclosure of the evaluation frame when the closure is used.
54
 
55
  There is an optional further component if the closure has been
75377 hornik 56
  byte-compiled.  This is not normally user-visible, but is indicated
58825 ripley 57
  when functions are printed.
6994 pd 58
}
58825 ripley 59
 
1248 maechler 60
\seealso{
58825 ripley 61
  \code{\link{args}}.
6994 pd 62
 
58825 ripley 63
  \code{\link{formals}}, \code{\link{body}} and
64
  \code{\link{environment}} for accessing the component parts of a
65
  function.
66
 
38930 maechler 67
  \code{\link{debug}} for debugging; using \code{\link{invisible}} inside
68
  \code{return(.)} for returning \emph{invisibly}.
1248 maechler 69
}
24330 ripley 70
\references{
88581 hornik 71
  \bibshow{R:Becker+Chambers+Wilks:1988}
24330 ripley 72
}
1323 maechler 73
\examples{
74
norm <- function(x) sqrt(x\%*\%x)
75
norm(1:4)
76
 
77
## An anonymous function:
61168 ripley 78
(function(x, y){ z <- x^2 + y^2; x+y+z })(0:7, 1)
1323 maechler 79
}
286 maechler 80
\keyword{programming}