Return Values क्या हैं
एक return value वह answer है जो एक function वापस देता है, एक vending machine से आपका snack गिरने की तरह। अगर आप यह नहीं कहते कि क्या return करना है, R आखिरी चीज़ वापस देता है जो इसने work out की।
In this page:
Syntax
function_name <- function(arg) {
# body
return(value)
}
Return Values
एक function की value या तो वह है जो return() explicitly return करता है, या -- अगर return() कभी call न हो -- इसकी last evaluated expression की value।
इस implicit return behavior की वजह से, कई छोटे R functions return() पूरी तरह omit करते हैं। function की complexity के आधार पर दोनों styles common और idiomatic हैं।
Note:
Simple one-expression functions के लिए, R idiom अक्सर return() skip करता है और बस last expression की value implicitly return होने देता है।
उदाहरण: Return Values
square_explicit <- function(n) {
return(n * n)
}
square_implicit <- function(n) {
n * n
}
cat(square_explicit(5), square_implicit(5), "\n")
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- function में
return()के बाद code रखना, जो कभी नहीं चलता। - last expression पर depend करना जब यह
y <- n * nजैसा एक assignment हो, जो value invisibly return करता है, इसलिए कुछ print नहीं होता। return(a, b)से कई values return करना, जो एक error है;list(a = a, b = b)जैसी एक list return करें।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: