Function के Arguments
Arguments वे ingredients हैं जो आप हर बार इस्तेमाल करते वक्त एक function को देते हैं, एक ice cream order करते वक्त flavor और size की तरह। आप इन्हें order में या नाम से pass कर सकते हैं।
In this page:
Syntax
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
greet <- function(name, times) {
for (i in 1:times) {
cat("Hello,", name, "!\n")
}
}
greet("Sam", 2)
greet(times = 1, name = "Priya")
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. #}
आम गलतियां
- एक required argument भूल जाना, जैसे बिना
timesकेgreet("Sam")call करना, जो सिर्फtimesइस्तेमाल होने पर error देता है। - बिना names के arguments गलत order में pass करना, जैसे
greet(2, "Sam"), जो2को नाम की तरह इस्तेमाल करता है। 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: