R Return Values

A return value is what a function sends back to the caller after it finishes. In R, every function returns something — either explicitly using return(), implicitly as the last evaluated expression, or NULL if there is nothing to return. Knowing how to return single values, multiple values, and structured outputs makes your functions far more useful.

Explicit Return

double <- function(x) {
  result <- x * 2
  return(result)    # explicit return
}

double(7)   # 14

Implicit Return (Last Expression)

triple <- function(x) {
  x * 3    # no return() — last expression is returned automatically
}

triple(5)   # 15

Both approaches work. Use return() for clarity when the function has multiple exit points or complex logic.

Early Return

Use return() to exit a function before reaching the end — useful for validation:

safe_divide <- function(a, b) {
  if (b == 0) return(NA)     # exit early if division by zero
  return(a / b)
}

safe_divide(10, 2)    # 5
safe_divide(10, 0)    # NA (no error, just NA)

Returning Multiple Values Using a List

R functions can return only one object. To return multiple values, wrap them in a list.

describe_vector <- function(x) {
  return(list(
    count  = length(x),
    mean   = mean(x),
    median = median(x),
    sd     = round(sd(x), 2),
    range  = range(x)
  ))
}

scores <- c(85, 92, 78, 95, 60, 88)
result <- describe_vector(scores)

cat("Count:", result$count,  "\n")
cat("Mean: ", result$mean,   "\n")
cat("SD:   ", result$sd,     "\n")

Output:

Count: 6
Mean:  83.
SD:    12.81

Unpacking List Returns

stats <- describe_vector(c(10, 20, 30, 40, 50))

# Access by name
stats$mean      # 30

# Assign individual parts to separate variables
m <- stats$mean
s <- stats$sd
r <- stats$range

Returning a Data Frame

student_summary <- function(names, scores) {
  grades <- ifelse(scores >= 75, "Pass", "Fail")
  return(data.frame(Name = names, Score = scores, Grade = grades))
}

result <- student_summary(
  names  = c("Asha","Balu","Cena"),
  scores = c(88, 65, 92)
)
print(result)

Output:

   Name Score Grade
1  Asha    88  Pass
2  Balu    65  Fail
3  Cena    92  Pass

invisible() — Silent Return

Use invisible() to return a value without printing it automatically. This is common in functions that primarily perform an action (like writing a file) but also return a result for chaining.

save_and_return <- function(x) {
  # ... save x to file ...
  invisible(x)    # returns x but doesn't print it
}

result <- save_and_return(42)
result   # 42 — can still be accessed

Return Value Flow Diagram

Caller:  result <- my_function(inputs)
                         │
              ┌──────────┘
              │
         my_function runs
              │
         return(value)
              │
              └──────────► result now holds value

Designing good return values is just as important as designing good inputs. Functions that return structured lists or data frames are especially powerful — they let callers choose which parts of the output they need and chain the results into further analysis steps.

Leave a Comment

Your email address will not be published. Required fields are marked *