2

I have found the following solution on here about the post: https://stackoverflow.com/a/34327262/2994949

The user eipi10 uses = insted of <- to assign a value to the corrFunc function. Why does he do this?

Also, he/she creates the data.frame in the next line, but does not use a return to have that data.frame returned from the code. The function works, so I wonder why and how.

EDIT Does it provide any advantages to used or not to use the returncommand? This is something that has not been answered before, that's why I think this is not a duplicate.

I tried to ask this in a comment, but I need 50 reputation to put comments and why I put an answer in the initial thread, it was immediately deleted. Could anybody tell me, how to ask about a solution I find in a thread when I can not comment and can not post an answer?

Thank you.

EDIT

The first part of my question has been answered partly by the link but I still do not understand why the return is avoided. thanks :)

Community
  • 1
  • 1
Cactus
  • 864
  • 1
  • 17
  • 44
  • 5
    http://stackoverflow.com/questions/1741820/assignment-operators-in-r-and – rawr Oct 01 '16 at 12:58
  • 1
    For the part on `return()` http://stackoverflow.com/questions/11738823/explicitly-calling-return-in-a-function-or-not/ – jogo Oct 02 '16 at 11:56

1 Answers1

2

From ?return:

If the end of a function is reached without calling return, the value of the last evaluated expression is returned.

For example,

f <- function() {
   x <- 1
   x
}

is equivalent to the same function with return(x) as the last statement. Perhaps surprisingly,

f <- function() {
   x <- 1
}

also returns the same value, but returns it invisibly. There is a minor schism (perhaps not quite as strong as the = vs. <- schism) about whether it's better practice to always use an explicit return(): I believe it is good practice (because it makes the intention of the code more explicit), but many old-school R programmers prefer the implicit return value.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Does it provide any advantages using an explicit return? Just wondering. – Cactus Oct 01 '16 at 14:05
  • only stylistic/readability as far as I know – Ben Bolker Oct 01 '16 at 14:06
  • Ok, so since I am newby and I am trying to develop a good style, is it recommend to use it or is it considered redundant code which should be left out? – Cactus Oct 01 '16 at 14:07
  • It depends who you ask! I would recommend it but other experienced R users would consider it redundant. – Ben Bolker Oct 01 '16 at 14:08
  • ok I will use it then, it helps me. Thanks and have a good day. – Cactus Oct 01 '16 at 14:13
  • Google's R style guide doesn't mention whether to use `return` or not, but its example function does https://google.github.io/styleguide/Rguide.xml#examplefunction - some other style guides out there recommend using `return`, and I can't find any that say "dont". – Spacedman Oct 01 '16 at 14:41
  • @Spacedman The [tidyverse styleguide](https://style.tidyverse.org/functions.html#return) says not to use it. Regardless of style guide, a lot of the core R package code (but not all of it) omits it. And, finally, given that R is a functional programming language where *every function call has a value*, using `return` is inconsistent with the mental model of R execution logic. – Konrad Rudolph Feb 13 '20 at 10:33