# User Defined Function in R Programming: Creation & Use

Instructor: Alexis Kypridemos

Alexis is a technical writer for an IT company and has worked in publishing as a writer, editor and web designer. He has a BA in Communication.

R has several built in functions. But sometimes you need to create your own functions for a specific purpose. This lesson explains how to create such user-defined functions in R programming. We will also explain how to use those functions in R statements and in other R functions.

## User-Defined Functions

In R programming, user-defined functions are functions created by the user, as opposed to the built-in functions included in R. The mean() function, for example is a built-in function, which calculates the mean of a set of values. If you need a certain functionality, that can be re-used, which is not readily available in R, then you can create it on your own.

## How to Create a User-Defined Function

You can create any function by building on the following general structure:

functionName <- function (arguments){

commands to perform

}

#### Example

userDefinedFunction <- function(x){
result <- x * 10
print(result)
}
x <- 385
userDefinedFunction(x)

Let us understand the above code line by line.

userDefinedFunction <- function(x){ - userDefinedFunction is an arbitrary name for the new function. <- is the assignment operator. In R syntax, this states that what follows is the definition of the element userDefinedFunction, which has not yet been specified as a function. function(x){ specifies that userDefinedFunction is a function, and that it takes a single argument, or input, noted as x. The left curly bracket { indicates that what follows will be the commands that the function will perform on the input, x.

result <- x * 10 - the command. Defines the arbitrarily named variable result and uses the assignment operator <- to give the variable the input value multiplied by 10: x * 10. In other words, the purpose of the function is to take an input and produce an output that is ten times larger.

print(result) - the second command. print() is another built-in R function. It displays the value in the parentheses. In this case, it displays the result of the command in the function, stored in the variable named result.

This is a very simple example. There is no limit to the number of commands that you can write inside the function. You an write complex logic or also invoke another user-defined function from within a function.

} - closes the function definition.

x <- 385 - This and the next line are not part of the function; they have been added to demonstrate how the function works. This first line gives the arbitrary value of 385 to the variable x.

userDefinedFunction(x) - Having been defined above, the user-defined function userDefinedFunction will execute its commands (multiply input value by ten and display the results) for value x, which in this case has been defined in the above line as 385. When this second line of code is executed, it should produce the result 3850.

## How to Use a User-Defined Function

A user-defined function can be used in combination with other R functions and statements as explained below.

### How to Use a User-Defined Function in a built-in R Function

The following is an example of using the previously created user-defined function with the built-in R function mean():

userDefinedFunction <- function(x){
result <- x * 10
print(result)
}
x <- list(85, 86, 87, 87, 90, 93, 107, 54, 99)
userDefinedFunction(mean(as.numeric(c(x))))

Let us understand the function invocation.

x <- list(85, 86, 87, 87, 90, 93, 107, 54, 99) - Defines the variable x as a list, containing nine numeric values. Note that list() is another built-in R function, like mean().

userDefinedFunction(mean(as.numeric(c(x)))) - Instructs the user-defined function userDefinedFunction to take as its input the calculated mean of the list x. In other words, the mean will be calculated first for the numbers contained in the list x, and then the userDefinedFunction will take that mean and multiply it by 10 and print the result.

To unlock this lesson you must be a Study.com Member.

### Register to view this lesson

Are you a student or a teacher?

### Unlock Your Education

#### See for yourself why 30 million people use Study.com

##### Become a Study.com member and start learning now.
Back
What teachers are saying about Study.com

### Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.