1

I have a question about global assignment. Here is a small example. I would like to ask why the second global assignment do not affect inside the function?And why the return output of function is different from what I call for outside the function after I run the function)

Thanks!

enter image description here enter image description here

1 Answers1

1

Here's my explanation, it's very complicated but I've tried my best:

  • in a_function in the first line you assigned X to the matrix of 3 in the global environment
  • in line two you took X, added every element by 4 and then assigned it to X in the global environment (with <<-), overriding the previous value of X in the global environment. But more interestingly (and difficult to see / untuit from the code), is what happens to X in the function environment. Due to lazy evaluation, now is when R finds a value for X within the function environment - it evaluates the promise, which is to evaluate the expression X in the enclosing environment of the function. We just bound the matrix of 3's to X in the global environment. So now the value of X in the function environment is also the matrix of 3s.
  • This explains why when you call a_function(X) it returns the matrix of 3s.
  • Then when the expression X is evaluated in the global environment, it returns the matrix of 7s (because you globally assigned it there from within your function using <<-).
Josh White
  • 1,003
  • 1
  • 17
  • Thank you for explanation. I don't understanding the sentence **"it evaluates the promise, which is to evaluate the expression X in the enclosing environment of the function"**. Could you please explain it further? – littletennis Nov 19 '22 at 16:22
  • And I add one example for comparison, take a look, thanks! – littletennis Nov 19 '22 at 16:23
  • 1
    See here for a discussion about promises in R: https://adv-r.hadley.nz/functions.html#promises. Basically, they are what power R's lazy evaluation, in which an argument to a function is not evaluated at the start of the function call, but when that argument is first accessed. – Josh White Nov 19 '22 at 18:35
  • 1
    And your new example follows the exact same process as your first one, but is a bit more simple. When `X` is first referenced in `X+3`, R has to find what value binds to `X` in the function. It does so by looking into the global environment (which is the enclosing environment of the function) for the value of X which at that time is the 1->9 matrix. This remains the binding of X within the function, even though you change what values binds to X outside the function which you alter with the superassignment (`<--`). – Josh White Nov 19 '22 at 18:44
  • 1
    You have to remember that you have two different values bound to X. the value bound to X within the function, and the value bound to X outside the function in the global environment. Using `<--` only` changes the binding outside the function. And when you refer to X inside the function it looks first to the value bound to X inside the function. – Josh White Nov 19 '22 at 18:45
  • Thank you for you patient! Please check my understanding: 1. The value of a specific variable in the function environment is initiate when it is first called inside the function. So for the example of `X+3` and `X+4`, the final output is 1:9 +4, because when X+3 is called, the X inside function environment has been initiate(say X1). So X+4 will use this X1. And X+3 only change the X in the global envir, but not inside function envir. 2. for another example, the first line X=3 do not initiate X, since it don't call it, so X+4 need to use X in the global environment, which is 3. Am I correct? – littletennis Nov 19 '22 at 22:22
  • 1
    Yes, that’s explanation is good! – Josh White Nov 20 '22 at 02:20
  • Hi Josh, I have a new question. If there is no input argument for the function, then even I "initiate"(the first time I call it) X in the function with global assignment, every time I call X will directly use the X from global environment nor function environment, am I correct? It seems that there are no function environment anymore. – littletennis Nov 20 '22 at 18:32