\HeaderA{function}{Function Definition}{function}
\aliasA{return}{function}{return}
\keyword{programming}{function}
\begin{Description}\relax
These functions provide the base mechanisms for defining
new functions in the \R{} language.
\end{Description}
\begin{Usage}
\begin{verbatim}
function( arglist ) expr
return(value)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{arglist}] Empty or one or more name or name=expression terms.
\item[\code{value}] An expression.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The names in an argument list can be back-quoted non-standard names
(see \sQuote{\LinkA{backquote}{backquote}}).

If \code{value} is missing, \code{NULL} is returned.  If it is a
single expression, the value of the evaluated expression is returned.
(The expression is evaluated as soon as \code{return} is called, in
the evaluation frame of the function and before any
\code{\LinkA{on.exit}{on.exit}} expression is evaluated.)

If the end of a function is reached without calling \code{return}, the
value of the last evaluated expression is returned.
\end{Details}
\begin{Section}{Warning}
Prior to \R{} 1.8.0, \code{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,
\code{a=foo()} names the list component \code{a} and gives it the value
which results from evaluating \code{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.  Supply a (named) list \code{value} instead.
\end{Section}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{args}{args}} and \code{\LinkA{body}{body}} for accessing the arguments
and body of a function.

\code{\LinkA{debug}{debug}} for debugging; using \code{\LinkA{invisible}{invisible}} inside
\code{return(.)} for returning \emph{invisibly}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
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)
\end{ExampleCode}
\end{Examples}

