R Functions in Detail
Jul 20, 2019 | Updated: Jul 20, 2019
# R

This article explains everything about functions in R programming languages, such as what R functions are, how to use them, how to create user-defined functions in R, and much more.

What is Function?

In programming, a function is a set of instructions (or, a piece of code) to carry out a specific task. Functions are defined to be used repeatedly and called when needed. A function can or cannot accept arguments or parameters. Also, a function can or cannot return one or more values.

Functions in R

R has a big number of built-in functions that you can use to do sophisticated tasks. For example, you can calculate square root of a number by calling sqrt built-in function.

> sqrt(16)
[1] 4

As you can see, to call a function you just have to write the name of the function with arguments or parameters in parentheses.

Note that the argument you pass into the function can be a raw data, a R object, or the result of another R function.

Function Definition

Users can create their own functions (called user-defined functions) in R. To write a user-defined function in R the basic syntax is as follows:

function_name <- function(argument_list){

As you can see a function definition has three basic parts:

  • function_name is the actual name of the function which is stored as an object in R. Note that a function in R is an object.
  • argument_list is a placeholder to pass any number of data into the function. Note that arguments are optional in R function. That is, a function may or may not contain arguments. Also, arguments can have default values.
  • a_function_body_of_code is a set of statements to accomplish the dedicated task of the function.

Example of User-defined Function

The following lines of code present a user-defined function which takes two arguments, x and y, and calculate x raised to the power of y.

# Function definition 
x_power_y <- function(x, y){
    result <- x^y

Note that the arguments used in function definition are called formal arguments, but when arguments are passed while calling the function are called actual arguments.

Function Call (by Position & by Name)

A function can be called by supplying the arguments in the same order as defined in the function (by position). Or, a function can be called by supplying the arguments in a different order but with the name of the formal arguments as defined in the function (by name). For example,

# Call the function by position of the arguments
> x_power_y(2, 3)
[1] 8

# Call the function by names of the arguments
> x_power_y(y = 3, x = 2)
[1] 8

Default Values for Formal Arguments

In R function declaration, you can assign default values to the formal arguments (if you want). This makes the arguments optional when calling the function. For example,

# Function definition with default values for the arguments
x_power_y <- function(x = 2, y = 3){
    result <- x^y

# Call function with empty argument list
# By default x = 2, y = 3
> x_power_y()
[1] 8

# Call function with assigning x argument only
# By default y = 3
> x_power_y(4)
[1] 64

# Call function with assigning y argument only
# By default x = 2
> x_power_y(y = 7)
[1] 128

# Call function with assigning both x and y arguments
> x_power_y(4, 4)
[1] 256

R Function Return Value (Explicit & Implicit)

R function can return back the result of the function explicitly or implicitly. Explicit return from a R function is accomplished with the built-in return() function in R.

But if there is no explicit return from a function with the return() then the value of the last statement or expression or line of the function body code is return automatically in R, which is implicit return.

The following code snippets illustrate the same function in two versions: fist version returns value explicitly and second version returns value implicitly.

# Function definition with explicit return
x_power_y <- function(x, y){
  result <- x^y
  return (result)

# Function call and output
> x_power_y(3, 3)
[1] 27
# Function definition with implicit return
x_power_y <- function(x, y){
  result <- x^y

# Function call and output
> x_power_y(3, 3)
[1] 27

As you can see, both versions of the function are equivalent. So, you can write either way but explicit return enhance the readability of the code.

Final Talks

Huh! That's a bit long! But I hope that would be pretty useful for the beginners to understand functions in R. Isn't it? I thought so!

Happy R coding!


You are welcome to write comments, suggestions, corrections, or any queries related to the article. Your comments may take some time to be appeared. Please be aware that any irrelevant comments will be deleted. Thanks for your understanding, and your respectful & relevant comments!