Introduction
In this article, I will demonstrate how to create and use functions in R. A function is simply a set of statements that have been put together to perform a specific task. With functions, a code can be broken into simpler parts that are easy to understand and maintain. R comes with many in-built functions and it also allows its users to create their functions.
In R, functions are treated as objects, meaning that the R interpreter is capable of passing control to the function, together with the arguments. The function will then perform its tasks and pass control and results to the interpreter.
Function definition
To define a function in R, we use the function keyword. It takes the syntax given below.
function_name <- function(argument_1, argument_2, ...) {
Function body
}
The following are the different parts of the function,
Function name
This is the actual name of the function. It is stored as an object with that name in the R environment.
Arguments
Once the function has been invoked, a value will be passed to the argument. The arguments can have default values, but arguments are optional in a function.
Function body
This is a collection of statements that define what the function will do.
Return value
This is the last expression in our function body which is to be evaluated.
R allows us to define our functions. Each user-defined function is specific to what the user needs and we can use them just like the in-built functions.
The example given below shows how to create and use a function in R.
# A function to return the squares of numbers in a sequence.
new.function <- function(x) {
for (j in 1:x) {
y <- j^2
print(y)
}
}
It will return the following output.
-> new.function(4)
[1] 1
[1] 4
[1] 9
[1] 16
>
We have defined a function named new. A function that takes one argument x. The function will create a sequence from the value of this argument and get the square of each number in the sequence.
Here is another example
-> pow <- function(a, b) {
+ # function to print a raised to the power b
+ result <- a^b
+ print(paste(a, "raised to power", b, "is", result))
+ }
>
The above function will return the following output.
-> pow(2, 3)
[1] "2 raised to power 3 is 8"
>
Above, we have defined a function named pow. The function takes two arguments, a and b. The function will then get the result of raising a to power of b. The result of this will be assigned to the variable named result.
Function calls
We have mentioned a function call. A function call is done using the name of the function. If the function accepts arguments, then they should be passed during the function call.
Let us demonstrate how a function is called in R. We will use the function we created earlier, the new. function().
# A function to return the squares of numbers in a sequence.
>
> new.function <- function(x) {
+ for (j in 1:x) {
+ y <- j^2
+ print(y)
+ }
+ }
>
Call the function new. function() and pass the argument 5 as an argument.
new.function(5)
The program will return the following when executed.
> new.function(5)
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
>
Also, we can call a function without using an argument. The example given below best demonstrates this.
# A function to return the squares of numbers in a sequence.
> new.function <- function(x) {
+ for (j in 1:5) {
+ y <- j^2
+ print(y)
+ }
+ }
>
Call the function new.function() and pass no argument to it.
new.function()
Execution of the above program will return the following output.
> new.function()
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
>
When calling a function, we can specify the value of the arguments using either their names or position. This means that the arguments have to be supplied in the same order that they were defined in the function, or maybe supplied in a different sequence but assigned by use of their names.
Consider the example given below.
# A function that takes 3 arguments.
> new.function <- function(x, y, z) {
+ result <- x * y + z
+ print(result)
+ }
>
Call the function by the position of arguments,
new.function(4, 3, 12)
Execution of the above program will give the following output.
> new.function(4,3,12)
[1] 24
>
Calling the function by the names of arguments.
new.function(x = 10, y = 7, z = 2)
Execution of the above program will give the following output.
> new.function(x = 10, y = 7, z = 2)
[1] 72
>
We can have default arguments for a function. With such, we can call the function without passing arguments to it, and the default arguments will be used to provide the result.
At the same time, we can pass in new values and this will allow us to get another result rather than the default one.
Consider the example given below.
> # Create a function with default arguments.
> new.function <- function(x = 2, y = 6) {
+ result <- x * y
+ print(result)
+ }
>
Call the function without passing arguments to it,
new.function()
Execution of the above program will return the following,
> new.function()
[1] 12
>
Call the function and pass values for the arguments,
new.function(7,5)
Execution of the above program will return the following.
> new.function(7,5)
[1] 35
>
In the first call, we did not pass arguments to the function. The default values of the arguments were used to calculate the result. In the second call to the function, we passed new values to the arguments, 7 and 5. These were used to calculate the result.
Return function
When creating functions, we will mostly want to have the function do some processing and return some results to us. In R, we can return some results using the return() function. The function takes the syntax given below,
- return(expression)
The function can return any value which is a valid R object. Let us create an example that demonstrates how to use this function.
>
> checkFunc <- function(y) {
+ if (y > 0) {
+ result <- "y is Positive"
+ }
+ else if (y < 0) {
+ result <- "y is Negative"
+ }
+ else {
+ result <- "y is Zero"
+ }
+ return(result)
+ }
>
Let us perform sample runs by calling the checkFunction() function and passing some arguments to it. They will run as follows.
> checkFunc(2)
[1] "y is Positive"
> checkFunc(-3)
[1] "y is Negative"
> checkFunc(0)
[1] "y is Zero"
>
If we don’t have explicit returns from any function, the value obtained from the last expression to be evaluated will be returned automatically.
The following example demonstrates this.
> checkFunc <- function(y) {
+ if (y > 0) {
+ result <- "y is Positive"
+ }
+ else if (y < 0) {
+ result <- "y is Negative"
+ }
+ else {
+ result <- "y is Zero"
+ }
+ result
+ }
>
They will run as follows
> checkFunc(2)
[1] "y is Positive"
>
Explicit return() functions should only be used when we need to return a value immediately from a function. If it’s not the function’s last statement, it will end the function prematurely and control will shift to the place where it was called.
The following example demonstrates this
> checkFunc <- function(y) {
+ if (y > 0) {
+ return("y is Positive")
+ }
+ else if (y < 0) {
+ return("y is Negative")
+ }
+ else {
+ return("y is Zero")
+ }
+ }
>
They will run as follows
> checkFunc(2)
[1] "y is Positive"
> checkFunc(-3)
[1] "y is Negative"
> checkFunc(0)
[1] "y is Zero"
>
In the example given above, in case y > 0, then the function will immediately return y is Positive without going further to evaluate the rest of the function body.
Summary
In this article, I will demonstrate how to create and use functions in R. I demonstrated how to define a function, how to call a function without arguments or with arguments, and pass values for the arguments. I also explained how to use a predefined return function in R. Proper coding snippets along with output are provided.