Return vs. Paste vs. Print vs. Cat

Return vs. Paste vs. Print vs. Cat

par Leanna Lui,
Nombre de réponses : 1

When we write a function, is it always necessary to "return" a value? For example, could we write a function ...

avg_1 <- function(x) {

[meat of function]

}

and in this case, I haven't put return, paste, print or cat? 

In other words, is it necessary to supplant a command once I've written the function so that an output is produced? 

En réponse à Leanna Lui

Re: Return vs. Paste vs. Print vs. Cat

par Erik Spence,
It is only necessary to return a value from a function when you want to get the value out of the function and use it somewhere else. Otherwise you may use print or cat to print out a message of some kind. However, DO NOT use print as a substitute for return. If you are intending to return a value, then return it. Don't print it and accidentally return it as well, which is a side-effect of using print at the end of a function.