CASE 1:
x <- 10
f <- function(x){
x <- 20
x}
f(x)
# [1] 20
x
# [1] 10
I am satisfied with the output.
CASE 2:
x <- 10
f <- function(x){
x <<- 20
x}
f(x)
# [1] 20
x
# [1] 20
I expect the output of f(x) to be 10 not 20 because function f should return local value of x i.e. 10. I am totally confused.
CASE 3:
x <- 10
f <- function(x){
x <<- 20
x}
f(10)
# [1] 10
x
# [1] 20
In this case f(10)
returns 10 not 20 as in CASE 2. What is going on?