← Back to R Course | Chapter 5: Functions | Lesson 4 of 7

Return Values

In this page:

  1. Return Values

Return Values

A function's value is either whatever return() explicitly returns, or -- if return() is never called -- the value of its last evaluated expression. Because of this implicit return behavior, many short R functions omit return() entirely. Both styles are common and idiomatic depending on the function's complexity.

Note: For simple one-expression functions, R idiom often skips return() and just lets the last expression's value be returned implicitly.

Example: Return Values

markup
square_explicit <- function(n) {
  return(n * n)
}

square_implicit <- function(n) {
  n * n
}

cat(square_explicit(5), square_implicit(5), "\n")
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.