Celdor - 1 year ago 60
R Question

# Understanding evaluation of input arguments of functions

I am reading Advanced R by Hadley Wickham where some very good exercises are provided. One of them asks for description of this function:

``````f1 <- function(x = {y <- 1; 2}, y = 0) {
x + y
}
f1()
``````

Can someone help me to understand why it returns 3? I know there is something called lazy evaluation of the input arguments, and e.g. another exercise asks for description of this function

``````f2 <- function(x = z) {
z <- 100
x
}
f2()
``````

and I correctly predicted to be 100;
`x`
gets value of
`z`
which is evaluated inside a function, and then x is returned. I cannot figure out what happens in
`f1()`
, though.

Thanks.

In summary, if the parameter does not have user-specified value, its default value will be evaluated in the function's evaluation frame. So `y` is not evalulated at first. When the default of `x` is evaluated in the function's evaluation frame, `y` will be modified to 1, then `x` will be set to 2. As `y` is already found, the default argument has no change to be evaluated. if you try `f1(y = 1)` and `f1(y = 2)`, the results are still `3`.