r-novice-inflammation icon indicating copy to clipboard operation
r-novice-inflammation copied to clipboard

adding code example to "Automatic return"

Open jnvaughn opened this issue 6 years ago • 2 comments

Would be nice to see something like this examples associated with the side note:

So, an alternative way of writing the previous function would be: fahrenheit_to_kelvin <- function(temp_F) { ((temp_F - 32) * (5 / 9)) + 273.15 }

jnvaughn avatar Jun 19 '18 19:06 jnvaughn

Good point, but I wonder whether we can turn that side note into something even more useful ;-) How about a challenge that leads learners to the solution you posted above. They would have to figure out then, that the intermediate temp_K assignment can be removed as well. What do you think?

Either way: do you want to start a PR about this against 02-func-R.Rmd? Either with your initial suggestion, or the challenge idea?

katrinleinweber avatar Jun 19 '18 19:06 katrinleinweber

I'm curious whether it's actually worth the time to make this point in the episode. I've found that some people familiar with other languages before learning R can find R code using automatic returns somewhat unintuitive. I think part of this is because functions without return statements can look like functions that are supposed to be modifying objects in place (which rarely happens in R, data.tables being the main exception I've encountered, but does happen in other languages).

Since there is no functional difference between an automatic return and using a return statement , might it be simpler to leave out the note about automatic returns (or just avoid spending additional time)? I think the main counterargument would be that it could help learners when they encounter other peoples' code with automatic returns, but this seems like a marginal gain for people trying to initially get started with R.

engal avatar Jun 02 '19 19:06 engal