R: How is the argument passed to the function by default?

In .GlobalEnv, I defined the following variable and function

x = 0; foo <- function(t=x) {x=1; t} 

When I called the function in the following ways

 foo() # gives 1 foo(t=x) # gives 0 

Can someone explain this? Thanks!!!

+6
source share
1 answer

To make a comment in response:

In the first case, the function knows that it needs to pass x to t . He searches for x in his lexical domain and finds x=1 . In the second case, you pass x=0 from the global environment, so it no longer searches for it in the lexical domain and passes it directly to t .

+1
source

Source: https://habr.com/ru/post/976993/


All Articles