0 votes
in R Basics by
What is f(3) where:

y <- 5

f <- function(x) { y <- 2; y^2 + g(x) }

g <- function(x) { x + y }

Why?

1 Answer

0 votes
by

12. In f(3), y is 2, so y^2 is 4. When evaluating g(3), y is the globally scoped y (5) instead of the y that is locally scoped to f, so g(3) evaluates to 3 + 5 or 8. The rest is just 4 + 8, or 12.

...