Get startedGet started for free

What are functions?

1. What are functions?

So, what is a function? You have used

2. Examples of functions

dozens of functions in R already, mean, plot, and ncol are all examples of them, but how were they created and what are they doing?

3. Elements of a function

Essentially, good functions are lines of code written to do a few key things. They accept arguments, the input to the function or the options that can be controlled by them. They have a body where the code is executed, and that body performs one thing well. By that, I mean that a single function does not try and do an entire analysis, but is instead a piece of the analysis that works together with other functions. They also return a value, and the class of that returned value is predictable and stable. Defined in this way, functions are building blocks for analysis. In fact, every operation in R is at some level a function call. After this chapter, you will feel comfortable with learning to use new functions, and write your own simple functions for financial analysis. For a more in depth explanation of functions, check out the DataCamp course Writing Functions in R, by Hadley Wickham.

4. Function documentation

Let's start simple, with documentation. If you run into a new function, and don't know how to use it, where do you turn to learn more? R has an extensive built in documentation system that you can take full advantage of. The quickest way to access it is to type a question mark, and then the name of the function, to be taken straight to the help page for that function! From there, you can read about what arguments it takes, an overview of what it does, the values it returns, and potentially some examples of how to use it. Here, we have searched for the help page for matrix, and from there we can learn all about how to use it.

5. Function arguments

You already know how to use functions, but you might not know much about the arguments to the functions you are using. Arguments are the input to your function. There are two overarching types of arguments, required and optional. Required arguments have to be explicitly given as input for the function to run, otherwise you get an error. Often, required arguments are the data that the function acts on. Optional arguments are ones that come with a default value, but that value can often be modified to unlock additional functionality.

6. Function arguments example

Let's work through an example to get a feel for this. Suppose you have a vector of stock returns, and you want to calculate the mean return. What happens if you just call mean by itself? Of course, it complains that the argument x is missing, this is because x is required. Alright, let's pass returns into the mean function. The first argument to mean is x, so we don't have to specify x equals returns, but instead let R match the argument by position. Now what happens? Well that's not right! We wanted R to ignore the NA value and just compute the mean for the other three returns, but it didn't remove it. Let's check the documentation for mean to see if we can figure this out. Reading the documentation, we find that there is an optional argument, na-dot-rm that is set to FALSE by default. If it was true, the NA's would be stripped out first before the calculation. Back in the script, we specify na-dot-rm equals TRUE. It's good practice to let R match your required arguments by position, but to explicitely set your optional arguments by name. Now mean returns what we want.

7. Let's practice!

This process of learning from the documentation is common in R, especially when you are introduced to new functions. Time to practice it with some more exercises.