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

Function के Arguments

Arguments वे ingredients हैं जो आप हर बार इस्तेमाल करते वक्त एक function को देते हैं, एक ice cream order करते वक्त flavor और size की तरह। आप इन्हें order में या नाम से pass कर सकते हैं।

In this page:

  1. Function Arguments
Syntax
r
function_name <- function(arg1, arg2) {
  # body
}

function_name(value1, value2)          # by position
function_name(arg2 = value2, arg1 = value1)   # by name

Function Arguments

Arguments function की parameter list में declare होते हैं और call site पर या तो positionally या नाम से pass हो सकते हैं।

Named arguments किसी भी order में दिए जा सकते हैं, जो कई parameters वाले functions के लिए खासकर useful है। Positional और named arguments mix करना allowed है, जब तक positional वाले पहले आएँ।

Note: Named arguments से call करना, जैसे f(name = "Sam", times = 3), ज़्यादा readable और order-independent है।

उदाहरण: Function Arguments

markup
greet <- function(name, times) {
  for (i in 1:times) {
    cat("Hello,", name, "!\n")
  }
}

greet("Sam", 2)
greet(times = 1, name = "Priya")
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. एक required argument भूल जाना, जैसे बिना times के greet("Sam") call करना, जो सिर्फ times इस्तेमाल होने पर error देता है।
  2. बिना names के arguments गलत order में pass करना, जैसे greet(2, "Sam"), जो 2 को नाम की तरह इस्तेमाल करता है।
  3. greet(nam = "Sam") जैसे एक named argument misspell करना, जो partially match हो सकता है या unused argument error के साथ fail हो सकता है।
🔒

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.