Scope modifiers: Difference between revisions

Content deleted Content added
Tikkanz (talk | contribs)
Added R code
Line 281: Line 281:
>>> </lang>
>>> </lang>
More information on the scope modifiers can be found [http://docs.python.org/3.0/reference/simple_stmts.html#grammar-token-global_stmt here].<br>
More information on the scope modifiers can be found [http://docs.python.org/3.0/reference/simple_stmts.html#grammar-token-global_stmt here].<br>

=={{header|R}}==
R uses lexical scoping &ndash; there is a nice introduction to this in the [http://cran.r-project.org/doc/FAQ/R-FAQ.html#Lexical-scoping FAQ on R]. There are no keywords related to scoping, but the [http://stat.ethz.ch/R-manual/R-patched/library/base/html/assign.html assign] function allows you to choose the environment in which a variable is assigned. The global assignment operators , '<<-' and '->>' are effectively shorthand for 'assign(..., envir=globalenv()).
<lang r>
x <- "defined in global env"
foo <- function()
{
# Print existing value
print(x) # "defined in global env"
# define a local value and print it
x <- "defined inside foo"
print(x) # "defined inside foo"
# remove local value, reassign global value from within foo
rm(x)
assign("x", "reassigned global value", envir=globalenv())
print(x) # "reassigned global value"
}
foo()
</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==