A function is a set of statements organized together to perform a specific task. R has a large number of in-built functions but we may as well create functions by our own.


Built-in Functions

R has many in-built functions which can be directly called without defining them beforehand. In base R your find, functions such as seq(), mean(), max(), min(), sum(), paste(), length() and sample(), among others.

s <- seq(1:5000)
mean(s)
## [1] 2500.5
max(s)
## [1] 5000
min(s)
## [1] 1
sum(s)
## [1] 12502500
paste("The sequence 's' contains", length(s), "components")
## [1] "The sequence 's' contains 5000 components"

A function consists of different parts:

Consider the sample() function, which takes a sample of the specified size from the elements of a vector (either with or without replacement). Type help("sample") into your console for further information.

We may ask R for the arguments of the sample() function by using the args() command.

args(sample)
## function (x, size, replace = FALSE, prob = NULL) 
## NULL

If we want to retrieve the function body we we just type the function name (without parenthesis!) into the R console. With respect to our example, we type sample into our console.

sample
## function (x, size, replace = FALSE, prob = NULL) 
## {
##     if (length(x) == 1L && is.numeric(x) && is.finite(x) && x >= 
##         1) {
##         if (missing(size)) 
##             size <- x
##         sample.int(x, size, replace, prob)
##     }
##     else {
##         if (missing(size)) 
##             size <- length(x)
##         x[sample.int(length(x), size, replace, prob)]
##     }
## }
## <bytecode: 0x7f87a0266288>
## <environment: namespace:base>

In order to apply the sample() function to pick 5 numbers with replacement from the sequence s, which we defined above, we type sample(s, size = 5, replace = TRUE) into the R console.

sample(s, size = 5, replace = TRUE)
## [1] 4816 3128 1665 3374 3284

User-defined Function

It is very easy to create user-defined functions in R. A function in R is created by using the keyword function. The basic syntax of a function definition is as follows

function_name <- function(arg_1, arg_2, ...) {
   Function body
}

Let us create a function to print the cube of the number 3 \((3^3)\). Note that this function does not need any arguments.

print.cube3 <- function() {
  rv <- 3^3 # return value
  print(rv)
  }

In order to call the function print.cube3() we simply type print.cube3() into the R console.

print.cube3()
## [1] 27

This function is for sure not of much use, hence we extend the function and compute the cube of any scalar given by the user \((x^3)\).

print.cube <- function(s) {
  cubed <- s^3
  print(cubed)
  }

In order to call the function print.cube(), this time we have to provide the argument s.

print.cube(2)
## [1] 8

Calling a Function with Argument Values (by position and by name)

For real life applications functions often have several arguments. It is worth noting, that in R the arguments to a function call can be supplied in the same sequence as defined in the function or they can be supplied in a different sequence but assigned to the names of the arguments.

To showcase this behavior we define a function to do some simple computations.

my.function <- function(a,b,c) {
  result <- a * b - c
  print(result)
}

Now, we can call the function by position of arguments, where a = 2, b = 10 and c = 1. We have to make sure that the numbers are given to the function in correct order.

my.function(2,10,1)
## [1] 19

We would get the same result by calling the function by names of the arguments, however, this time we do not need to care about the specific order of the given arguments.

my.function(b = 10, c = 1, a = 2)
## [1] 19

Calling a Function with Default Argument

Another convenient feature of a function is that we can define the value of the arguments in the function definition and call the function without supplying any argument to get the default result. But we can also call such functions by supplying new values of the argument and get non default result.

# Create a function with arguments.
my.multiply <- function(a = 15, b = 3) {
  result <- a * b
  print(result)
}

Now we can either call the function without giving any argument.

my.multiply()
## [1] 45

Or we may call the the function and provide new values for the arguments.

my.multiply(5, 5)
## [1] 25