Hello, > > gives you 120, but you cannot access it after the end of execution. >
Because you're just printing the final value of 'a', not returning it. fac <- function(x){ a <- 1 for(i in 1:x) a <- a*i a } The return value must be the last instruction in a function. Then, if you want, you can assign it to a variable that exists in the environment the function was called from. This is the usual practice, and it makes sense. Once a function is written you don't have to worry with what happens inside it. The caller's environment, it's variables, should not be changed. That's why '<<-' is not recommended. You might have variables with surprise values. Read, like said earlier, the R Inferno, Circle 6: "What's so wrong about global assignments? Surprise. Surprise in movies and novels is good. Surprise in computer code is bad." And download it. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Using-FUNCTION-to-create-usable-objects-tp4588681p4590617.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.