[R] Trying to learn how to write a function... can't define a variable??

Berend Hasselman bhh at xs4all.nl
Fri Sep 7 21:59:56 CEST 2012


On 07-09-2012, at 21:54, William Dunlap wrote:

> The functions
>   f1 <- function(x) x <- x + 1
> and
>   f2 <- function(x) x + 1
> both return the same value.  You can see
> this by doing
>   z1 <- f1(10) ; print(z1)
>   z2 <- f2(10) ; print(z2)
> or just
>   print(f1(10))
>   print(f2(10))
> 
> f1(x) does not automatically print its output when run
> directly from the command line while f2(x) does because
> assignment statements inhibit autoprinting.

Ouch. 
And it explains why my version with (x<-x+1) did print the result.

Thanks.

Berend




More information about the R-help mailing list