Scope क्या है
In this page:
x <- global_value
function_name <- function() {
x <- local_value # local to the function
x
}
Scope
एक function के अंदर बने variables उस function के execution के लिए local हैं और इसके return होते ही गायब हो जाते हैं, बाहर same नाम वाले variables को प्रभावित किए बिना।
R lexical scoping इस्तेमाल करता है, मतलब एक function अपने enclosing environment से variables पढ़ सकता है, लेकिन function के अंदर assignments default से outer वाले को modify करने की बजाय नए local variables बनाते हैं। <<- operator explicitly एक enclosing scope में एक variable modify कर सकता है, हालाँकि यह sparingly इस्तेमाल होता है।
<- से एक variable को assign करना एक नया local variable बनाता है -- यह function के बाहर same नाम वाला variable modify नहीं करता।उदाहरण: Scope
x <- "global value"
show_scope <- function() {
x <- "local value"
cat("Inside function:", x, "\n")
}
show_scope()
cat("Outside function:", x, "\n")
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि एक function के अंदर
x <- "local"global variable बदल देगा, जब यह एक local बनाता है। - casually globals बदलने के लिए
<<-इस्तेमाल करना, जो code follow करना मुश्किल बनाता है। - return होने के बाद एक function के अंदर defined एक variable इस्तेमाल करना, जो
object not foundदेता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: