In R, the preferred method is to assign the result to a new object: fac <- function(x) { a<-1 for(i in 1:x){ a<-a*i print(a) } a # need to explicitly state what the function should return }
myresult <- fac(5) myresult Sarah On Thu, Apr 26, 2012 at 2:16 PM, michaelyb <cel81009...@gmail.com> wrote: > Ista, > > Since you seem to know your stuff very well, how would you get 120 out of a > function that gives you the factorial of 5, without using factorial(5)? > >> Meanwhile, look at this example instead: >> fac<-function(x){a<-1 >> for(i in 1:x){ >> a<-a*i >> print(a)}} > > gives you 120, but you cannot access it after the end of execution. > > -- Sarah Goslee http://www.functionaldiversity.org ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.