## 2.5 Functions

Functions are chunks of code that are given a name so that they can be easily used multiple times. Perhaps without realising it, you have used functions already many times!

### 2.5.1 Defining your own function

A function is composed of the following elements:

• a name: in R functions are objects just like vectors or matrices and they are given a name.

• arguments: these are objects that will be used within the function.

• body: a chunk of code which is run within the function.

• output: an object that the function returns.

Let’s consider an example.

my.function <- function(x,y){
z <- x + y
return(z)
}

The above function computes the sum of two numbers x and y. Let’s call it.

my.function(2,3)
##  5

The sum between 2 and 3 is indeed 5.

Let’s look at the code line by line. In the first line, we assigned a function using the command function to an object called my.function. my.function has two arguments called x and y. Then there is an opening curly bracket {. The last line of code has a closing curly bracket }: whatever is in between the two brackets is a chunk of code which is run when the function is run. The second line computes a new variable called z which stores the sum of x and y. The third line of code tells us that the function should return z as output.

Let’s consider a slightly more complicated function.

new.function <- function(x,y){
z1 <- x^2
z2 <- z1 + y
return(z2)
}

The new.function returns the sum between the square of the first input x and the second input y. Let’s call the function.

new.function(2,3)
##  7
new.function(3,2)
##  11

Notice that new.function(2,3) is different from new.function(3,2): indeed in the fist case the sum between 2^2 and 3 is computed, whilst in the second the sum between 3^2 and 2 is computed. Furthermore, that the variable z1 exists only within the function: when you call the function the output does not create a variable z1. The output does not create either a variable z2 it simply returns the value that is stored in z2, which can the be assigned as in the following example.

value <- new.function(2,3)
value
##  7

We stored in value the output of new.function(2,3).

An equivalent way to write new.function is as follows:

new.function <- function(x,y){
x^2 + y
}
new.function(2,3)
##  7

The output is the same. We did not create any variable within the function and we did not explicitly use the return command. R understands that the last line of code is what the function should return.

### 2.5.2 Calling functions

In R functions can be called in various ways. Before we have seen function calls as

new.function(2,3)

How did it work?

• The function new.function has a first argument x and a second argument y.

• R matched the first argument in new.function(2,3) to x, that is x=2, and the second argument to y, that is y=3.

We could have also been more explicit and state what x and y were.

new.function(x=2, y=3)
##  7

So now explicitly we state that the input x of new.function is 2 and that the input y is 3. Notice that the two ways of specifying inputs give the exact same results.

### 2.5.3 Mathematical and statistical functions

The number of functions available in R is massive and it would be impossible to mention them all. Here I just give you a list of mathematical and statistical functions that we may use in the following.

• exp computes the exponential of the entries of an object

• log computes the logarithm of the entries of an object

• sqrt computes the square root of the entries of an

• sum computes the sum of the entries of an object

• abs computes the absolute value of the entries of an object

• mean computes the mean of the entries of an object

• sd computes the standard deviation of the entries of an object

• var computes the variance of the entries of an object