Does R automatically return function outputs, and if so is it automatically saved in the global environment?

Beginner here to coding, and just wanted to verify if my understanding of the 'return' statment for R functions is correct.

Basically, the return statement makes it so whatever output is intended to be created by the function is actually stored in the global environment for future use.

Without the return statement, the output would not be retained in the global environment, saved as variables for future use.

For example:

bargraph_creator <- function(data) {

if (data == "Group1") {
perdata.m <- melt(perdata[,c(1:5,9)],id = c("Time","Date"))
stringtitle <- "Group1"

} else if (data == "Group2") {
perdata.m <- melt(perdata[,c(1,6:9)], id = c("Time","Date"))
stringtitle <- "Group2"


In the above code, since no return statement is entered, if I entered in "Group1" as the input, my outputs (perdata.m and stringtitle) would not be returned?

Based on my understanding, your understanding is not perfect. The function can return a value without using the return keyword, as it returns the value of the last evaluated expression.

For example, consider the following example. It'll successfully print a named vector of 3 elements.

f1 <- function(x) {
    y <- x + 1

f2 <- function(x) {
    y <- x + 1

f3 <- function(x) {
    y <- x + 1

a <- f1(1)
b <- f1(1)
c <- f1(1)

print(c("a"=a, "b"=b, "c"=c))

Here's the relevant documentation:

If value is missing, NULL is returned. If it is a single expression, the value of the evaluated expression is returned. (The expression is evaluated as soon as return is called, in the evaluation frame of the function and before any on.exit expression is evaluated.)

If the end of a function is reached without calling return , the value of the last evaluated expression is returned.

Hope this helps.

To give an example closer to your example with an if block, the same logic applies. The following should create z successfully and then print out "xyz".

f <- function(x) {
    if (x == "abc") {
        y <- "pqr"
    } else {
        y <- "xyz"

z <- f("def")
1 Like

Thank you! Just to make sure I'm understanding your explanation correctly, the return statement is not necessary as R automatically gives us the value of the last evaluated expression in the body?

Furthermore, return does not 'save' the outputs of the function into the global environment, it simply makes it possible for us to assign variables eg. z <- f("def") to those outputs; so we can then assign those outputs ourselves into the global environment?

at the conclusion of a function a value is passed to the calling environment.
If that environment happens to be the global environment then the value is passed there. You can let it fly by and go unsaved by declining to assign the value to a name in the global environment, or you can trivially assign it to a name in the global environment as you no doubt have experienced most often.
But the global environment is not special over other environments that your function may be called from, its only 'special' in so far is that most likely when you work with R you are mostly instructing commands from the global environment, but you are not forced to.

1 Like

I see, thank you that answers my questions! Basically, if I want to save the values of my function's output and access it later, I should assign a trivial variable name to it in the global environment. Think I got it right but let me know if not, otherwise thanks for you and Yarnabrina's help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.