title: Difference between for loop and return in a function
body: <p>I have written two functions named f and f2 as follow:</p>
  
  <pre><code>f&lt;- function(n) {
    factorial &lt;- 1
    if (as.integer(n)!=n | n&lt;0)
      print("The factorial for this number does not exist")
    else if( n == 0 )
      print("Factorial of 0 is 1")
    else{
      return(n * factorial(n - 1))
      print(paste("The factorial of ",n,"is",factorial))}
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>f2&lt;-function(n){
    factorial&lt;-1
    if (as.integer(n)!=n | n&lt;0){
      print("The factorial of this number does not exist")
    }else if (n==0){
      print("The factorial of 0 is 1")
    }else {
      for (i in 1:n)
        factorial&lt;-factorial*i
      print(paste("The factorial of ",n,"is",factorial))
    }
  }
  </code></pre>
  
  <p>My functions (f and f2) do the same thing (factorial). For the first function, I used "return" and for the second function I used "for loop". But when I compute, for example, f(5) and f2(5), the type of their output is different. The second code (function f2) gives me </p>
  
  <pre><code>"The factorial of  5 is 120"
  </code></pre>
  
  <p>as output and this is what I want, but the first code (function f) gives me</p>
  
  <pre><code>120
  </code></pre>
  
  <p>as output, I don't know why the first code does not execute the print command. I would like to see "The factorial of  5 is 120" for the first code, I mean f(5), too.</p>