Create Function in R

“A code used to accomplish a certain goal or perform a specific task is called a function. That code can be called and reused numerous times in a program. Data can be passed as input to a function, which will return results. Functions are classified into two types (in-built functions and user-defined functions). The functions stored in a programming language’s functional library and have predefined tasks to perform in that language are known as in-built functions. On the other hand, user-defined functions are created by users to perform certain tasks in the program.”

Syntax to Write a Function in R in Ubuntu 20.04?

For creating the functions instreams in R programming language, we need to understand the following syntax:

function_name <- function (argument) {statement}

A reserved word in R “function” is used with round parenthesis to declare a function. Every function should be given a name. This name is used to invoke the function from anywhere inside a program. A function is stored by R as an object with its name. Arguments are reserved places for the inputs that a function could need. We just fed the correct values in the function’s arguments for calling a function. A function can have no or one or more arguments. The curly braces “{ }” are known body of a function is made up of instructions or statements inside the body. The code inside a function’s body is executed to achieve the function’s goal.

Create Functions in R Programming Language

The following examples show how we can create functions in the R programming language in Ubuntu 20.04. We will also implement some examples to show how to create functions of different types and arguments.

Example 1: Creating a Simple Function in R
In this example, we will create a function that will help us check whether the entered value is even or odd. There is no predefined function to perform this task, so that we will create it on our own.

Let’s name the function “oe_check” we will take “x” as an argument. The value will be passed to x from outside the function. We will write the code inside the body, as shown in the example below.

As you can see above, when we called the function and passed the value “3” as an argument. After executing the code, it generated an output of “odd.” When we passed 2 as an argument, it gave us an “even output.”

Example 2: Create Functions With Single Input and Single Output
In this example, we will create a function that will give the result as a single output by taking a single value as an argument.

Let’s create a function to calculate the area of a circle. This function will take the radius as an argument. We will name the function “circle_area,” and the radius value will be passed as an argument to the function.

We have passed a value of radius “4″ in the function, executed in the body where the formula to find the circle area is written. After executing the function, it shows us the output after calculating the area.

Example 3: Create Functions With Multiple Inputs and Multiple Outputs
In R Language, many functions accept input objects but only return one output object. However, this is not a limitation because we can create a list of multiple outputs and access them to the list element to get the output. Let’s create a function “rect,” which will take “l” and “w” as an argument and calculate the area and parameter of the field.

As R allows us to return a single object, we have created a list and assigned it to a variable “result.” When we pass the value to the function, the function will store the results in the list we created.

Example 4: Create Inline Functions in R
Sometimes, to save resources and improve the program’s execution, we can use the inline function. To create inline functions, we have to write the function command along with an argument. There is no need to create the body of the function; we can write the code/formula/expression just after the function’s argument.

The value will be passed to the variable “f” to which the function is assigned. It will generate the results whenever we call the function.

Example 5: How to Pass Arguments to a Function in R
You can pass arguments in different ways in R. The arguments can be passed in the identical order as specified in the function. You can also utilize the name of arguments to pass them in the function. The function will use its default arguments to execute the program if we don’t pass the argument. Let’s see these conditions in the example below:

As you can see in the 3rd condition, we have not passed any argument while calling the function, so the function used the defined arguments to execute the program.

Example 6: Lazy Evaluation of the Function in R
In R, the function will continue to be executed even if some parameters are absent, as long as the execution does not include those arguments.

Let’s explain it with the following example.

As the length is not required in the function so, the function will execute without any problem. If you don’t pass the argument and then use it in the function definition, you’ll get an error stating that “length” was not passed but was used in the function definition.


In this tutorial, we tried to explain the key concepts of functions in R. Now, you should be able to use functions in R but create user-defined functions according to your requirement. We implemented 6 examples in this post to teach you how to create simple functions in R, how to create functions with single and multiple inputs and outputs, how to create inline functions, how to pass the arguments to the function, and how a function is evaluated in R.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.