Variables declared inside a function only exist (unless passed) inside that function.
x <- 1
foo <- function(x) {
y <- 3
z <- x + y
return(z)
}
y
Error: object 'y' not found
Variables passed into a function and then reassigned are overwritten, but only inside the function.
foo <- function(x) {
x <- 2
y <- 3
z <- x + y
return(z)
}
foo(1)
x
5
1
Variables assigned in a higher environment than a function exist within that function, without being passed.
foo <- function() {
y <- 3
z <- x + y
return(z)
}
foo()
4