Why do bar and baz behave differently? When bar is called both the value of a printed and the value of a in the global scope are the same, but when baz is called the printed value and the value in the global scope are different. Seemingly, the only difference is that a is used (but not defined) in a parent environment.
a = 1:3
b = 4
foo <- function(a) {
a[1] <<- b
print(a)
}
bar <- function(a) {
foo(a)
}
baz <- function(a) {
a
foo(a)
}
bar(a) # 4 2 3
a # 4 2 3
a <- 1:3
baz(a) # 1 2 3
a # 1 2 3