| 42333 |
ripley |
1 |
% File src/library/base/man/function.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 59039 |
ripley |
3 |
% Copyright 1995-2012 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}
|
|
|
14 |
\special{return(value)}
|
| 2 |
r |
15 |
}
|
|
|
16 |
\description{
|
| 24330 |
ripley |
17 |
These functions provide the base mechanisms for defining
|
|
|
18 |
new functions in the \R language.
|
| 2 |
r |
19 |
}
|
| 6994 |
pd |
20 |
\arguments{
|
|
|
21 |
\item{arglist}{Empty or one or more name or name=expression terms.}
|
| 58825 |
ripley |
22 |
\item{expr}{An expression.}
|
| 24330 |
ripley |
23 |
\item{value}{An expression.}
|
| 6994 |
pd |
24 |
}
|
|
|
25 |
\details{
|
| 40456 |
ripley |
26 |
The names in an argument list can be back-quoted non-standard names
|
|
|
27 |
(see \sQuote{\link{backquote}}).
|
| 6994 |
pd |
28 |
|
| 24330 |
ripley |
29 |
If \code{value} is missing, \code{NULL} is returned. If it is a
|
|
|
30 |
single expression, the value of the evaluated expression is returned.
|
| 46383 |
ripley |
31 |
(The expression is evaluated as soon as \code{return} is called, in
|
|
|
32 |
the evaluation frame of the function and before any
|
|
|
33 |
\code{\link{on.exit}} expression is evaluated.)
|
| 61433 |
ripley |
34 |
|
| 24330 |
ripley |
35 |
If the end of a function is reached without calling \code{return}, the
|
|
|
36 |
value of the last evaluated expression is returned.
|
|
|
37 |
}
|
| 24322 |
ripley |
38 |
|
| 58825 |
ripley |
39 |
\section{Technical details}{
|
|
|
40 |
This type of function is not the only type in \R: they are called
|
|
|
41 |
\emph{closures} (a name with origins in LISP) to distinguish them from
|
|
|
42 |
\link{primitive} functions.
|
|
|
43 |
|
|
|
44 |
A closure has three components, its \code{\link{formals}} (its argument
|
|
|
45 |
list), its \code{\link{body}} (\code{expr} in the \sQuote{Usage}
|
|
|
46 |
section) and its \code{\link{environment}} which provides the
|
|
|
47 |
enclosure of the evaluation frame when the closure is used.
|
|
|
48 |
|
|
|
49 |
There is an optional further component if the closure has been
|
| 75377 |
hornik |
50 |
byte-compiled. This is not normally user-visible, but is indicated
|
| 58825 |
ripley |
51 |
when functions are printed.
|
| 6994 |
pd |
52 |
}
|
| 58825 |
ripley |
53 |
|
| 1248 |
maechler |
54 |
\seealso{
|
| 58825 |
ripley |
55 |
\code{\link{args}}.
|
| 6994 |
pd |
56 |
|
| 58825 |
ripley |
57 |
\code{\link{formals}}, \code{\link{body}} and
|
|
|
58 |
\code{\link{environment}} for accessing the component parts of a
|
|
|
59 |
function.
|
|
|
60 |
|
| 38930 |
maechler |
61 |
\code{\link{debug}} for debugging; using \code{\link{invisible}} inside
|
|
|
62 |
\code{return(.)} for returning \emph{invisibly}.
|
| 1248 |
maechler |
63 |
}
|
| 24330 |
ripley |
64 |
\references{
|
|
|
65 |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
|
|
|
66 |
\emph{The New S Language}.
|
| 47262 |
ripley |
67 |
Wadsworth & Brooks/Cole.
|
| 24330 |
ripley |
68 |
}
|
| 1323 |
maechler |
69 |
\examples{
|
|
|
70 |
norm <- function(x) sqrt(x\%*\%x)
|
|
|
71 |
norm(1:4)
|
|
|
72 |
|
|
|
73 |
## An anonymous function:
|
| 61168 |
ripley |
74 |
(function(x, y){ z <- x^2 + y^2; x+y+z })(0:7, 1)
|
| 1323 |
maechler |
75 |
}
|
| 286 |
maechler |
76 |
\keyword{programming}
|