← Back to R Course | Chapter 5: Functions | Lesson 4 of 7

Return Values क्या हैं

एक return value वह answer है जो एक function वापस देता है, एक vending machine से आपका snack गिरने की तरह। अगर आप यह नहीं कहते कि क्या return करना है, R आखिरी चीज़ वापस देता है जो इसने work out की।

In this page:

  1. Return Values
Syntax
r
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

markup
square_explicit <- function(n) {
  return(n * n)
}

square_implicit <- function(n) {
  n * n
}

cat(square_explicit(5), square_implicit(5), "\n")
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. #}
आम गलतियां
  1. function में return() के बाद code रखना, जो कभी नहीं चलता।
  2. last expression पर depend करना जब यह y <- n * n जैसा एक assignment हो, जो value invisibly return करता है, इसलिए कुछ print नहीं होता।
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.