Easy Tutorial
❮ R Java R Charts Cn ❯

R function

A function is a group of statements that together perform a task. R provides many built-in functions, and we can also create our own functions.

You can divide your code into different functions. How you divide the code into different functions is up to you, but logically, the division is usually based on each function performing a specific task.

A function declaration tells the compiler about the function's name, return type, and parameters. A function definition provides the actual body of the function.

In R, a function is an object that can have attributes.

Defining a Function

Function definitions in R use the function keyword and generally have the following form:

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

Explanation:

The return value of the function is specified using return().


Built-in Functions

R provides many useful built-in functions that we can use directly without defining them.

For example: seq(), mean(), max(), sum(x), and paste(...), etc.

Example

# Print all numbers from 32 to 44
print(seq(32, 44))

# Calculate the mean of two numbers
print(mean(25:82))

# Calculate the sum of all numbers from 41 to 68
print(sum(41:68))

Executing the above code will produce the following output:

[1] 32 33 34 35 36 37 38 39 40 41 42 43 44
[1] 53.5
[1] 1526

Custom Functions

We can create our own functions for specific functionalities and use them just like built-in functions.

Here are two examples of how to define custom functions:

Example

# Define a function to calculate the square of a series
new.function <- function(a) {
   for(i in 1:a) {
      b <- i^2
      print(b)
   }
}

Next, we can call the function:

Example

new.function <- function(a) {
    for(i in 1:a) {
       b <- i^2
       print(b)
    }
}

# Call the function and pass a parameter
new.function(6)

Executing the above code will produce the following output:

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36

We can also create a function without parameters:

Example

new.function <- function() {
    for(i in 1:5) {
        print(i^2)
    }
}       

# Call the function without passing parameters
new.function()

Executing the above code will produce the following output:

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25

Functions with Parameter Values

Function parameters can be passed in the order they were created, or they can be passed out of order but with specified parameter names:

Example

# Create a function
new.function <- function(a, b, c) {
   result <- a * b + c
   print(result)
}

# Without parameter names
new.function(5, 3, 11)

# With parameter names
new.function(a = 11, b = 5, c = 3)

Executing the above code will produce the following output:

[1] 26
[1] 58

Functions can also have default parameter values. If parameters are not passed during the call, the default values will be used:

Example

# Create a function with default parameters
new.function <- function(a = 3, b = 6) {
   result <- a * b
   print(result)
}

# Call the function without passing parameters, using defaults
new.function()

# Call the function with parameters
new.function(9, 5)

Executing the above code will produce the following output:

Lazy Evaluation Functions

Lazy evaluation postpones the computation until the result is needed. If the result is not needed, the computation will not be performed.

By default, R functions are lazily evaluated, meaning parameters are only computed when they are needed:

Example

f <- function(x) {
   10
}
f()

Executing the above code will produce the following output:

[1] 10

The above code executes without error, even though we did not pass a parameter. Since the function body does not use parameter x, it is not called, and no error occurs.

Example

new.function <- function(a, b) {
   print(a^2)
   print(a)
   print(b)  # b is used but not passed, so an error will occur
}

# Pass only one parameter
new.function(6)

Executing the above code will produce the following output:

[1] 36
[1] 6

Error in print(b): Missing argument "b" and no default value provided. Calls: new.function -> print Execution halted.

❮ R Java R Charts Cn ❯