There is always a return :) You just don't have to be explicit about it.
All R expressions return something. Including control structures and user-defined functions. (Control-structures are just functions, by the way, so you can just remember that everything is a value or a function call, and everything evaluates to a value).
For functions, the return value is the last expression evaluated in the execution of the function. So, for
f <- function(x) 2 + x
when you call f(3)
you will invoke the function +
with two parameters, 2
and x
. These evaluate to 2
and 3
, respectively, so `+`(2, 3)
evaluates to 5
, and that is the result of f(3)
.
When you call the return
function -- and remember, this is a function -- you just leave the control-flow of a function early. So,
f <- function(x) {
if (x < 0) return(0)
x + 2
}
works as follows: When you call f
, it will call the if
function to figure out what to do in the first statement. The if
function will evaluate x < 0
(which means calling the function <
with parameters x
and 0
). If x < 0
is true, if
will evaluate return(0)
. If it is false, it will evaluate its else
part (which, because if
has a special syntax when it comes to functions, isn't shown, but is NULL
). If x < 0
is not true, f
will evaluate x + 2
and return that. If x < 0
is true, however, the if
function will evaluate return(0)
. This is a call to the function return
, with parameter 0
, and that call will terminate the execution of f
and make the result 0
.
Be careful with return
. It is a function so
f <- function(x) {
if (x < 0) return;
x + 2
}
is perfectly valid R code, but it will not return when x < 0
. The if
call will just evaluate to the function return
but not call it.
The return
function is also a little special in that it can return from the parent call of control structures. Strictly speaking, return
isn't evaluated in the frame of f
in the examples above, but from inside the if
calls. It just handles this special so it can return from f
.
With non-standard evaluation this isn't always the case.
With this function
f <- function(df) {
with(df, if (any(x < 0)) return("foo") else return("bar"))
"baz"
}
you might think that
f(data.frame(x = rnorm(10)))
should return either "foo"
or "bar"
. After all, we return in either case in the if
statement. However, the if
statement is evaluated inside with
and it doesn't work that way. The function will return baz
.
For non-local returns like that, you need to use callCC
, and then it gets more technical (as if this wasn't technical enough).
If you can, try to avoid return
completely and rely on functions returning the last expression they evaluate.
Update
Just to follow up on the comment below about loops. When you call a loop, you will most likely call one of the built-in primitive functions. And, yes, they return NULL
. But you can write your own, and they will follow the rule that they return the last expression they evaluate. You can, for example, implement for
in terms of while
like this:
`for` <- function(itr_var, seq, body) {
itr_var <- as.character(substitute(itr_var))
body <- substitute(body)
e <- parent.frame()
j <- 1
while (j < length(seq)) {
assign(x = itr_var, value = seq[[j]], envir = e)
eval(body, envir = e)
j <- j + 1
}
"foo"
}
This function, will definitely return "foo"
, so this
for(i in 1:5) { print(i) }
evalutes to "foo"
. If you want it to return NULL
, you have to be explicit about it (or just let the return value be the result of the while
loop -- if that is the primitive while
it returns NULL
).
The point I want to make is that functions return the last expression they evaluate has to do with how the functions are defined, not how you call them. The loops use non-standard evaluation, so the last expression in the loop body you provide them might be the last value they evaluate and might not. For the primitive loops, it is not.
Except for their special syntax, there is nothing magical about loops. They follow the rules all functions follow. With non-standard evaluation it can get a bit tricky to work out from a function call what the last expression they will evaluate might be, because the function body looks like it is what the function evaluates. It is, to a degree, if the function is sensible, but the loop body is not the function body. It is a parameter. If it wasn't for the special syntax, and you had to provide loop bodies as normal parameters, there might be less confusion.
bind_rows(dats)
return
is only required explicitly when you want the function to exit and return something before the end of the function definition. Otherwise, the last command executed in the function is implicitlyreturn
ed.