# Do you use return

## Do you include return when you write functions?

• ### I always include return

Votes: 0 0.0%
• ### I only include return when doing an early exit from the function

Votes: 0 0.0%
• ### I never use return

Votes: 0 0.0%

• Total voters
4

#### Dason

##### Ambassador to the humans
When you write functions do you include a return statement? It isn't necessary in most cases since R will return the last evaluated expression as what it returns when it reaches the end of the function. I know some people feel passionately that since it isn't necessary that it's misguided and introduces clutter. Some people think it provides clarity. What are your opinions on include return. Here are some examples for either

Using returns:
C-like:
monitor_object <- function(object_name, FUN = dim){

# Get
fun_name <- deparse(substitute(FUN))
print(fun_name)
object_dim <- NULL
if(exists(object_name)){
object_output <- FUN(get(object_name))
}

callback_function <- function(...){
new_output <- NULL
if(exists(object_name)){
new_output <- FUN(get(object_name))
}
if(!identical(new_output, object_output)){

msg <- paste0(object_name, " changed. Summary function: ", fun_name,
"\nOld output: ", paste(object_output, collapse = " "),
"\nNew output: ", paste(new_output, collapse = " "))
object_output <<- new_output
message(msg)
}
return(TRUE)
}

return(callback_function)
}
without returns:
C-like:
monitor_object <- function(object_name, FUN = dim){

# Get
fun_name <- deparse(substitute(FUN))
print(fun_name)
object_dim <- NULL
if(exists(object_name)){
object_output <- FUN(get(object_name))
}

callback_function <- function(...){
new_output <- NULL
if(exists(object_name)){
new_output <- FUN(get(object_name))
}
if(!identical(new_output, object_output)){

msg <- paste0(object_name, " changed. Summary function: ", fun_name,
"\nOld output: ", paste(object_output, collapse = " "),
"\nNew output: ", paste(new_output, collapse = " "))
object_output <<- new_output
message(msg)
}
TRUE
}

callback_function
}

#### consuli

##### Member
Do not understand why C is releveant here.

However, I include return() most of the time.

#### Dason

##### Ambassador to the humans
Do not understand why C is releveant here.
Are you saying that because the code says "C-like"? There isn't an option for R but in terms of syntax highlighting I think the best you could describe R as with the options given is "C-like" since R does use a lot of similar keywords.

#### trinker

##### ggplot2orBust
I use it in an if statement to keep it one line rather than an else

#### Dason

##### Ambassador to the humans
I use it in an if statement to keep it one line rather than an else
I'm not sure exactly what you mean. Can you give an example? And is that the only case that you use it?

#### trinker

##### ggplot2orBust
I'm not sure exactly what you mean. Can you give an example? And is that the only case that you use it?
Code:
fun <- function(x, ...){

if (x < 5) return('Low')

'High'
}

fun(3)
## [1] "Low"

fun(8)
## [1] "High"

#### hlsmith

##### Less is more. Stay pure. Stay poor.
I use whatever the source code I am copying used.

#### Dason

##### Ambassador to the humans
What about
Code:
> f <- function(x, ...){if(x < 5) "Low" else "High"}
> f(3)
[1] "Low"
> f(7)
[1] "High"

#### GretaGarbo

##### Human
Now, it means "Code:" (e.g. trinkers last post)

#### Dason

##### Ambassador to the humans
If you don't specify a language then it just defaults to "Code". Specifying a language will do things like give you some syntax highlighting and give some indication which language the code is. There isn't an "R" option but the syntax of R is similar enough to C that the option "C-like" gives some appropriate syntax highlighting.

#### trinker

##### ggplot2orBust
What about
Code:
> f <- function(x, ...){if(x < 5) "Low" else "High"}
> f(3)
[1] "Low"
> f(7)
[1] "High"
Yeah but to me the return is more readable. Plus they can get nested. There may be 3-5 of these if() return() lines in there. It helps to avoid a bunch of nested if/elses.