R Functions

Introduction to R Functions

A function is just a block of code that you can call and run from any part of your program. They are used to break our code in simple parts and avoid repeatable codes.

You can pass data into functions with the help of parameters and return some other data as a result. You can use the function() reserve keyword to create a function in R. The syntax is:

func_name <- function (parameters) {
statement
}

Here, func_name is the name of the function. For example,

# define a function to compute power
power <- function(a, b) {
    print(paste("a raised to the power b is: ", a^b))
}

Here, we have defined a function called power which takes two parameters - a and b. Inside the function, we have included a code to print the value of a raised to the power b.


Call the Function

After you have defined the function, you can call the function using the function name and arguments. For example,

# define a function to compute power
power <- function(a, b) {
    print(paste("a raised to the power b is: ", a^b))
}

# call the power function with arguments
power(2, 3)

Output

[1] "a raised to the power b is:  8"

Here, we have called the function with two arguments - 2 and 3. This will print the value of 2 raised to the power 3 which is 8.

The arguments used in the actual function are called formal arguments. They are also called parameters. The values passed to the function while calling the function are called actual arguments.


Named Arguments

In the above function call of the power() function, the arguments passed during the function call must be of the same order as the parameters passed during function declaration.

This means that when we call power(2, 3), the value 2 is assigned to a and 3 is assigned to b. If you want to change the order of arguments to be passed, you can use named arguments. For example,

# define a function to compute power
power <- function(a, b) {
    print(paste("a raised to the power b is: ", a^b))
}

# call the power function with arguments
power(b=3, a=2)

Output

[1] "a raised to the power b is:  8"

Here, the result is the same irrespective of the order of arguments that you pass during the function call.

You can also use a mix of named and unnamed arguments. For example,

# define a function to compute power
power <- function(a, b) {
    print(paste("a raised to the power b is: ", a^b))
}

# call the power function with arguments
power(b=3, 2)

Output

[1] "a raised to the power b is:  8"

Default Parameters Values

You can assign default parameter values to functions. To do so, you can specify an appropriate value to the function parameters during function definition.

When you call a function without an argument, the default value is used. For example,

# define a function to compute power
power <- function(a = 2, b) {
    print(paste("a raised to the power b is: ", a^b))
}

# call the power function with arguments
power(2, 3)

# call function with default arguments
power(b=3)

Output

[1] "a raised to the power b is:  8"
[1] "a raised to the power b is:  8"

Here, in the second call to power() function, we have only specified the b argument as a named argument. In such a case, it uses the default value for a provided in the function definition.


Return Values

You can use the return() keyword to return values from a function. For example,

# define a function to compute power
power <- function(a, b) {
    return (a^b)
}

# call the power function with arguments
print(paste("a raised to the power b is: ", power(2, 3)))

Output

[1] "a raised to the power b is:  8"

Here, instead of printing the result inside the function, we have returned a^b. When we call the power() function with arguments, the result is returned which can be printed during the call.


Nested Function

In R, you can create a nested function in 2 different ways.

  • By calling a function inside another function call.
  • By writing a function inside another function.

Example 1: Call a Function Inside Another Function Call

Consider the example below to create a function to add two numbers.

# define a function to compute addition
add <- function(a, b) {
    return (a + b)
}

# nested call of the add function 
print(add(add(1, 2), add(3, 4)))

Output

[1] 10

Here, we have created a function called add() to add two numbers. But during the function call, the arguments are calls to the add() function.

First, add(1, 2) and add(3, 4) are computed and the results are passed as arguments to the outer add() function. Hence, the result is the sum of all four numbers.


Example 2: Write a Function Inside Another Function

Let's create a nested function by writing a function inside another function.

# define a function to compute power
power <- function(a) {
    exponent <- function(b) {
        return (a^b)
    }
    return (exponent)
}

# call nested function 
result <- power(2)
print(result(3))

Output

[1] 8

Here, we cannot directly call the power() function because the exponent() function is defined inside the power() function.

Hence, we need to first call the outer function with the argument a and set it to a variable. This variable now acts as a function to which we pass the next argument b.

Did you find this article helpful?