\HeaderA{formals}{Access to and Manipulation of the Formal Arguments}{formals}
\aliasA{formals<\Rdash}{formals}{formals<.Rdash.}
\keyword{programming}{formals}
\begin{Description}\relax
Get or set the formal arguments of a function.
\end{Description}
\begin{Usage}
\begin{verbatim}
formals(fun = sys.function(sys.parent()))
formals(fun, envir = environment(fun)) <- value
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{fun}] a function object, or see \sQuote{Details}.
\item[\code{envir}] environment in which the function should be defined.
\item[\code{value}] a list (or pairlist) of \R{} expressions.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
For the first form, \code{fun} can also be a character string
naming the function to be manipulated, which is searched for from the
parent environment.
If it is not specified, the function calling \code{formals} is used.

Only \emph{closures} have formals, not primitive functions.
\end{Details}
\begin{Value}
\code{formals} returns the formal argument list of the function
specified, as a \code{\LinkA{pairlist}{pairlist}}, or \code{NULL} for a
non-function or primitive.

The replacement form sets the formals of a function to the
list/pairlist on the right hand side, and (potentially) resets the
environment of the function.
\end{Value}
\begin{SeeAlso}\relax
\code{\LinkA{args}{args}} for a human-readable version,
\code{\LinkA{alist}{alist}},
\code{\LinkA{body}{body}},
\code{\LinkA{function}{function}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
require(stats); require(graphics)
length(formals(lm))      # the number of formal arguments
names(formals(boxplot))  # formal arguments names

f <- function(x) a+b
formals(f) <- alist(a=,b=3) # function(a,b=3)a+b
f(2) # result = 5
\end{ExampleCode}
\end{Examples}

