How to Write Functions in R with Default and Named Arguments

A function in programming is a block of standardized and reusable code used to execute a single operation. It provides greater modularity for your application. Several programming languages have different names for functions, for example, methods, subroutines, procedures, etc. Let’s see how to write a function in the R language.

Functions in R

Functions in R are used to logically break our code into smaller pieces, which become easy to maintain and understand. In R programming language, a function is an object, so the R interpreter can pass a command to a function, along with parameters that may be necessary for the function to execute certain actions.

There are two types of functions in R,

  1. The built-in functions
  2. User-defined functions

Syntax

function_name <- function (argument) {
     statement
}

Components

The function is a reserved keyword to declare a function.

The statements within the curly brackets create a body of the function. These brackets are optional if the function body carries only a single expression.

The function_name is a function object given a name by assigning it to a variable.

Return value: The function’s return value is the last expression in the function body to be evaluated.

Some of the inbuilt functions in R: seq(), mean(), max(), sum() and paste() etc.

Define a function in R

To define a function in R, use the function reserved keyword followed by brackets, which takes the function argument, and then it takes a statement or piece of code that executes after we call the function.

sum <- function(x, y) {
   result <- x + y
   print(paste("The sum of variable x and y is:", result))
}

Here, we created a function called sum().

It takes two arguments and prints the sum of two arguments.

We have used an inbuilt R paste() function, which is used to concatenate strings.

If we run the above code, then we will not get any output because to run the function, we need to call it first, and in the above code, we have not called a function yet.

How to call a function in R

To call a function in R, use its name, and then pass the arguments if it takes any arguments. That is it. Now, the function will be executed, and it will return the output.

sum <- function(x, y) {
 result <- x + y
 print(paste("The sum of variable x and y is:", result))
}

sum(19, 21)

Output

1] "The sum of variable x and y is: 40"

Here, the arguments x and y used in the function declaration are called formal arguments, and those used while calling the function are called actual arguments.

Assigning default values as arguments to a function

To assign default values as arguments of the function in R, provide an appropriate value to the formal argument in the function declaration.

sum <- function(x, y = 21) {
 result <- x + y
 print(paste("The sum of variable x and y is:", result))
}

sum(19)

Output

[1] "The sum of variable x and y is: 40"

If you pass the default argument while calling the function, then the calling function argument will override the default value.

sum <- function(x, y = 21) {
 result <- x + y
 print(paste("The sum of variable x and y is:", result))
}

sum(19, 18)

Output

[1] "The sum of variable x and y is: 37"

Named arguments in R function

If we use named arguments in the R function, then the argument’s order does not matter.

If we use named and unnamed arguments combine in the function, then named arguments are matched first, and then the remaining unnamed arguments are matched in positional order.

sum <- function(x = 19, y = 21) {
 result <- x + y
 print(paste("The sum of variable x and y is:", result))
}

sum()

Output

[1] "The sum of variable x and y is: 40"

Lazy Evaluation of Function

Function arguments are evaluated lazily, which means they are evaluated only when needed by the function body.

data <- function(x, y) {
 print(x ^ 2)
 print(x)
 print(y)
}

data(3)

Output

[1] 9
[1] 3
Error in print(y) : argument "y" is missing, with no default
Calls: data -> print
Execution halted

In this example, for the first two cases, there was no use of the y argument, so it returns the output. Still, for the last case, it uses the y argument, and we have not passed as a formal argument; that is why it returns an error saying: argument “y” is missing, with no default.

That is it for writing functions in R.

Leave a Comment