sep11.ppt

advertisement
Function and Environments
What’s going on inside a function
functions
• In R, functions are called by value, that is, the
value of the argument is copied into the
function
• In Fortran and C, functions are called by name,
that is, only pointers to the argument
variables are passed to the function
• R avoids some weird function behavior, but it
is wasteful in terms of space
Local and Global
• The value of any argument variable is local to
the function, as is the result of any assignment
to a variable inside the function
• If R cannot find a variable locally (it is global),
it will look outside the function for the value
• The function’s arguments and local variables
form an environment, nested within
environments global to it, all the way to the
top-level Global Environment
Environments
Global Environment
d
f1 →
f2 →
f1’s environment
x (argument)
v (local assignment)
f2’s environment
x (argument)
v, d (local assignment)
Download