function Return value

    function  Return value in R

This article shows how to apply the return command to produce outputs with user-defined R functions. The article contains three reproducible example. 

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)
}

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

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
}

…and the let’s apply it again:

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))
}

If we apply the function, we get the following list output:

my_fun3(x = 5, y = 3)                 # Apply function 3

 

Output Returned by User-Defined R Function

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:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you might want to have a look at the other R tutorials on my website:

At this point, you should have learned why and when to use the return command in R.

As a final note: In this tutorial we have returned a single value from our user-defined function. However, it would also be possible to use a similar R syntax to return other types of data objects such as a data frame, a tibble, a ggplot2 plot object, and so on…

Just leave me a comment below in case you have any further questions.

 

Subscribe to the  Gds tech educational and management system

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime 

Comments

Popular posts from this blog

📚Gds-Tech 📚 EMS

spreadsheet in excel

vocab