How to Define and Call the Functions in R

function, in a programming environment, is a set of instructions. A programmer builds the function to avoid repeating the same task or reduce complexity.

Functions in R

A function in R is an object that implements its task and returns control to the interpreter, and whatever result may be stored in other objects. A function is a set of statements constructed together to perform a particular task, and the R language has a large number of inbuilt functions, and the user can create their own functions.

Definition of R Function

function_name <- function(arg_1, arg_2, ...) {
     Function body 
}

Components of Function

There are four main components of functions in R.

  1. Function Name
  2. Function Parameters or Arguments
  3. Function Body
  4. Return Value

Function Name

This is the name of the function. It is stored in the R environment as an object with this name.

Function Parameters

The argument is a placeholder, and when the function is invoked, you pass a value to the argument. Arguments are optional, and arguments can have default values.

Function B0dy

The body of the function contains a collection of statements that defines what the function does.

Return Value

The return value of the function is the last expression in the function body to be evaluated.

How to Create a function in R

We can create a user-defined function using the above syntax. Let’s create a function and see how to use it.

app.func <- function(x) {
 print(x ^ x)
}

Calling a function

To call the function, use the app.func() supplying 3 as an argument.

app.func <- function(x) {
 print(x ^ x)
}
app.func(3)

Output

[1] 27

Let’s say the function accepts the argument, but at the time of the calling, we don’t pass any argument.

app.func <- function(x) {
 print(x ^ x)
}
app.func()

Output

Error in print(x^x) : argument "x" is missing, with no default
Calls: app.func -> print
Execution halted

The error is self explainable since we are not passing any argument, so it throws the argument missing error.

Calling a function without an argument

app.func <- function() {
  for (x in 1:5) {
    print(x ^ x)
  }
}
app.func()

Output

[1] 1
[1] 4
[1] 27
[1] 256
[1] 3125

Calling a function with default argument

We can define the value of the parameters in the function definition and call the function without supplying any argument to get the default result.

app.func <- function(x = 3, y = 3) {
   res <- x * y
   print(res)
}
app.func()

app.func(4, 4)

Output

[1] 9
[1] 16

Lazy Evaluation of Function

Arguments to functions are evaluated gradually, so they are evaluated only when needed by the function body.

app.func <- function(x = 3, y = 3) {
 print(x ^ y)
 print(x)
 print(y)
}

app.func(4)

Output

[1] 64
[1] 4
[1] 3

Inbuilt functions

R has a good amount of inbuilt functions that you can use to complete your task. We have covered some of the functions in this blog like mean(), median(), mode(), seq(), rep(), sum(), min, max(), paste(), head(), rbind(), and cbind().

That is it for functions in the R language.

Leave a Comment