Return Value from R Function (3 Examples)
This article shows how to apply the return command to produce outputs with user-defined R functions. The article contains three reproducible examples:
- Example 1: R Function with return
- Example 2: R Function without return
- Example 3: Return Multiple Values as List
Let’s dive in!
Example 1: R Function with return
This example shows a simple user-defined R function, which computes the sum of the two input values x and y.
The last row of code shows how to use the return command in R. We simply need to insert the desired output of our function between the parentheses of the return command:
my_fun1 <- function(x, y) { # R function with return z <- x + y return(z) } |
my_fun1 <- function(x, y) { # R function with return z <- x + y return(z) }
After running the previous R syntax, we can apply our user-defined function as follows:
my_fun1(x = 5, y = 3) # Apply function 1 # 8 |
my_fun1(x = 5, y = 3) # Apply function 1 # 8
We used the input values 5 and 3 and our function returned the value 8 (i.e. 5 + 3 = 8).
Looks good!
However, is the return command really needed? That’s what you will learn in the next example.
Example 2: R Function without return
Let’s delete the return command from our function of Example 1…
my_fun2 <- function(x, y) { # R function without return z <- x + y z } |
my_fun2 <- function(x, y) { # R function without return z <- x + y z }
…and the let’s apply it again:
my_fun2(x = 5, y = 3) # Apply function 2 # 8 |
my_fun2(x = 5, y = 3) # Apply function 2 # 8
Same output!
Question: Why do we need the return command?
Answer: R returns the last output of a function automatically. We therefore do not need to use the return explicitly.
However, using the return command is often considered as good practice, since it makes the R code easier to read and understand.
This is especially the case in more complex functions, e.g. when we are returning multiple values as a list…
Example 3: Return Multiple Values as List
For illustration, I will show you a slightly more complex example for the usage of return in R. Consider the following function:
my_fun3 <- function(x, y) { # Return multiple values z1 <- x + y z2 <- x * y return(list(z1, z2)) } |
my_fun3 <- function(x, y) { # Return multiple values z1 <- x + y z2 <- x * y return(list(z1, z2)) }
If we apply the function, we get the following list output:
my_fun3(x = 5, y = 3) # Apply function 3 |
my_fun3(x = 5, y = 3) # Apply function 3
Figure 1: Multiple Function Outputs Returned as List.
The more complex our function gets, the more helpful is the return command. We could simply go back to our function and search for return( to get a quick overview of our output.
Therefore, I recommend to use return in every user-defined function. It’s not much programming work, but makes our lives much easier!
Further Resources
Do you want to learn more about user-defined functions in R? Then I can recommend the following YouTube video of Hefin Rhys:
Furthermore, you might want to have a look at the other R tutorials on my website:
- Return Multiple Objects from User-Defined Function in R
- List of R Functions
- The R Programming Language
At this point, you should have learned why and when to use the return command in R. However, just leave me a comment below in case you have any further questions.
Subscribe to my free statistics newsletter: