Return Values
In this page:
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
square_explicit <- function(n) {
return(n * n)
}
square_implicit <- function(n) {
n * n
}
cat(square_explicit(5), square_implicit(5), "\n")
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: