Function Overview

A very useful feature of the R environment is the possibility to expand existing functions and to easily write custom functions. In fact, most of the R software can be viewed as a series of R functions.

Syntax to define function

myfct <- function(arg1, arg2, ...) { 

Syntax to call functions

myfct(arg1=..., arg2=...)

The value returned by a function is the value of the function body, which is usually an unassigned final expression, e.g.: return()

Function Syntax Rules


  • Functions are defined by
    1. The assignment with the keyword function
    2. The declaration of arguments/variables (arg1, arg2, ...)
    3. The definition of operations (function_body) that perform computations on the provided arguments. A function name needs to be assigned to call the function.


  • Function names can be almost anything. However, the usage of names of existing functions should be avoided.


  • It is often useful to provide default values for arguments (e.g.: arg1=1:10). This way they don’t need to be provided in a function call. The argument list can also be left empty (myfct <- function() { fct_body }) if a function is expected to return always the same value(s). The argument ... can be used to allow one function to pass on argument settings to another.


  • The actual expressions (commands/operations) are defined in the function body which should be enclosed by braces. The individual commands are separated by semicolons or new lines (preferred).


  • Functions are called by their name followed by parentheses containing possible argument names. Empty parenthesis after the function name will result in an error message when a function requires certain arguments to be provided by the user. The function name alone will print the definition of a function.


  • Variables created inside a function exist only for the life time of a function. Thus, they are not accessible outside of the function. To force variables in functions to exist globally, one can use the double assignment operator: <<-


Define sample function

myfct <- function(x1, x2=5) { 
	z1 <- x1 / x1
	z2 <- x2 * x2
        myvec <- c(z1, z2) 

Function usage

Apply function to values 2 and 5

myfct(x1=2, x2=5) 
## [1]  1 25

Run without argument names

myfct(2, 5) 
## [1]  1 25

Makes use of default value 5

## [1]  1 25

Print function definition (often unintended)

## function(x1, x2=5) { 
## 	z1 <- x1 / x1
## 	z2 <- x2 * x2
##         myvec <- c(z1, z2) 
##         return(myvec)
## }

Previous page.Previous Page                     Next Page Next page.