[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  formals: Access to and Manipulation of the Formal Arguments

##  Description

Get or set the formal arguments of a ` [ function
](/link/function?package=base&version=3.6.2) ` .

##  Usage

    
    
    formals(fun = sys.function(sys.parent()), envir = parent.frame())
    formals(fun, envir = environment(fun)) <- value

##  Arguments

fun

a ` [ function ](/link/function?package=base&version=3.6.2) ` , or see
‘Details’.

envir

` [ environment ](/link/environment?package=base&version=3.6.2) ` in which the
function should be defined (or found via ` [ get
](/link/get?package=base&version=3.6.2) () ` in the first case and when ` fun
` a character string).

value

a ` [ list ](/link/list?package=base&version=3.6.2) ` (or ` [ pairlist
](/link/pairlist?package=base&version=3.6.2) ` ) of  R  expressions.

##  Value

` formals ` returns the formal argument list of the function specified, as a `
[ pairlist ](/link/pairlist?package=base&version=3.6.2) ` , or ` 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.

##  Details

For the first form, ` fun ` can also be a character string naming the function
to be manipulated, which is searched for in ` envir ` , by default from the
parent frame. If it is not specified, the function calling ` formals ` is
used.

Only _closures_ have formals, not primitive functions.

##  See Also

` [ formalArgs ](/link/formalArgs?package=base&version=3.6.2) ` (from  methods
), a shortcut for ` names(formals(.)) ` . ` [ args
](/link/args?package=base&version=3.6.2) ` for a human-readable version, ` [
alist ](/link/alist?package=base&version=3.6.2) ` , ` [ body
](/link/body?package=base&version=3.6.2) ` , ` [ function
](/link/function?package=base&version=3.6.2) ` .

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fformals&utm_source=r-docs&utm_medium=docs&utm_term=formals&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    require(stats)
    formals(lm)
    
    ## If you just want the names of the arguments, use formalArgs instead.
    names(formals(lm))
    methods:: formalArgs(lm)     # same
    
    ## formals returns a pairlist. Arguments with no default have type symbol (aka name).
    str(formals(lm))
    
    ## formals returns NULL for primitive functions.  Use it in combination with
    ## args for this case.
    is.primitive(`+`)
    formals(`+`)
    formals(args(`+`))
    
    ## You can overwrite the formal arguments of a function (though this is
    ## advanced, dangerous coding).
    f <- function(x) a + b
    formals(f) <- alist(a = , b = 3)
    f    # function(a, b = 3) a + b
    f(2) # result = 5
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fformals&utm_source=r-docs&utm_medium=docs&utm_term=formals&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)