Creating functions in R

Developed with

The course is part of this learning path

Fundamentals of R
Start course


Course Description 

This module looks at functions, how to create functions, and how they can be used in R.  

Learning Objectives 

The objectives of this module are to provide you with an understanding of: 

  • How to define a function in R 
  • How to use built in functions in R  
  • What a return value is  
  • That functions can be stacked, and that they do not require an input 
  • That arguments can be named or  

Intended Audience 

Aimed at all who wish to learn the R programming language. 


No prior knowledge of R is assumed 

Delegates should already be familiar with basic programming concepts such as variables, scope and functions 

Experience of another scripting language such as Python or Perl would be an advantage 

Understanding mathematical concepts will be beneficial 


We welcome all feedback and suggestions - please contact us at to let us know what you think. 


- [Instructor] We can create our own functions in R. There are three items that we need to understand in order to do so. The first being the name or what we call my function, as an example. It should be evocative, expressive or meaningful. Meaning that if you were to read the name of the function you would automatically know what the function is about. Say for example I asked for the mean function and I asked for the help of that, we understand when we see the keyword mean. That is by default, the arithmetic mean. Say for example I pulled up the sum, I would know that that was the sum of whatever vector I applied to it. The second part of a function is the arguments, the inputs, what does the actual function require? Potentially term the argument list. Here we have the arguments one and two as examples but in say for the case of the sum function or the mean function in the help window I can show you that there are three or four different parameters available. The third item that you need when defining a function is the body, the actual code that you wish to conduct. Now this can be stored as a single line expression or it could be a code in a block. But we term this the body of the function. Here I'll show you an example of what this would look like without any actual values, where the my function is the name of the function spelt m-y underscore f-u-n. The arguments are called arg1 and arg2 and the body can be over multiple lines of code. The assignment operator is used in the same way as we word for a vector being assigned to a variable. We here now in this case we are assigning the function to a variable. Say for example I'd like to create the function add_one, I could create the function in the same way we have up here the my_fun I have the name of my function as being evocative or expressive or meaningful and now I have use of the keyword function to create the actual function. I state that the arguments that this function will require are called x and after that I can run my addition or my body of code as following. In this case I would like to add one. I could have also defined this function quite simply as being in one line without the curly brackets. This is just a case of whether or not you would like to use an expression or whether you would like to use a code block over multiple lines. When we use a function we call a function. Call and use are the same they can be used interchangeably. And at this stage here I can say add one to the number three. I could say add one to the number three where x is equal to three. In more detailed function maybe I could use is how to I'd like to create a function that raises a number in this case x to the power two and I would like over multiple lines where I do various complicated things like add zero and add by zero and multiply by a number and so on and so forth. I'm here just showing you how you could have multiple lines of code in the body but captured within a pair of brackets. And I can raise three to the power two by calling the function power two and asking for this to be returned and instead let's say I said X equals 10 I could see that this is a 100. Let's say I wanted to create a function with two parameters or two arguments x raised to the power y, I could then use two inputs or two arguments to my function. Ask for the result as being stored as being x to the power y assigning that to a variable result and then returning the statement, utilizing the paste function inside of print to help it look decent on the screen. And I have x as one of my inputs, I have y as one of my inputs and then I explain to the user what has actually occurred. So now if I was to call this function or use this function, I can then ask for say three raised to the power two, three raised to the power two which is nine. I could repeat this by showing you 10 raised to the power two using the positional arguments. Alternatively I could have done this by name using x and y. And still receiving the same monster back as being a 100.

About the Author
Learning paths1

Kunal has worked with data for most of his career, ranging from diffusion markov chain processes to migrating reporting platforms.  

Kunal has helped clients with early stage engagement and formed multi week training programme curriculum. 

Kunal has a passion for statistics and data; he has delivered training relating to Hypothesis Testing, Exploring Data, Machine Learning Algorithms, and the Theory of Visualisation. 

Data Scientist at a credit management company; applied statistical analysis to distressed portfolios. 

Business Data Analyst at an investment bank; project to overhaul the legacy reporting and analytics platform. 

Statistician within the Government Statistical Service; quantitative analysis and publishing statistical findings of emerging levels of council tax data. 

Structured Credit Product Control at an investment bank; developing, maintaining, and deploying a PnL platform for the CVA Hedging trading desk.