The R Project SVN R

Rev

Rev 24322 | Rev 40456 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 r 1
\name{function}
2
\title{Function Definition}
3
\usage{
4
function( arglist ) expr
5
return(value)
6
}
7
\alias{function}
8
\alias{return}
9
\description{
24330 ripley 10
  These functions provide the base mechanisms for defining
11
  new functions in the \R language.
2 r 12
}
6994 pd 13
\arguments{
14
    \item{arglist}{Empty or one or more name or name=expression terms.}
24330 ripley 15
    \item{value}{An expression.}
6994 pd 16
}
17
\details{
24330 ripley 18
  In \R (unlike S) the names in an argument list cannot be quoted
19
  non-standard names.
6994 pd 20
 
24330 ripley 21
  If \code{value} is missing, \code{NULL} is returned.  If it is a
22
  single expression, the value of the evaluated expression is returned.
24322 ripley 23
 
24330 ripley 24
  If the end of a function is reached without calling \code{return}, the
25
  value of the last evaluated expression is returned.
26
}
27
\section{Warning}{
28
  Prior to \R 1.8.0, \code{value} could be a series of non-empty
29
  expressions separated by commas.  In that case the value
30
  returned is a list of the evaluated expressions, with names set to
31
  the expressions where these are the names of \R objects. That is,
32
  \code{a=foo()} names the list component \code{a} and gives it value
33
  the result of evaluating \code{foo()}.
24322 ripley 34
 
24330 ripley 35
  This has been deprecated (and a warning is given), as it was never
36
  documented in S, and whether or not the list is named differs by S versions.
6994 pd 37
}
1248 maechler 38
\seealso{
39
  \code{\link{args}} and \code{\link{body}} for accessing the arguments
40
  and body of a function.
6994 pd 41
 
1248 maechler 42
  \code{\link{debug}} for debugging; \code{\link{invisible}} for
43
  \code{return(.)}ing \emph{invisibly}.
44
}
24330 ripley 45
\references{
46
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
47
  \emph{The New S Language}.
48
  Wadsworth \& Brooks/Cole.
49
}
1323 maechler 50
\examples{
51
norm <- function(x) sqrt(x\%*\%x)
52
norm(1:4)
53
 
54
## An anonymous function:
55
(function(x,y){ z <- x^2 + y^2; x+y+z })(0:7, 1)
56
}
286 maechler 57
\keyword{programming}