← Back to Ruby Course | Chapter 7: Methods | Lesson 2 of 7

Parameters क्या हैं

Parameters वे blanks हैं जो एक method आपको call करते वक्त भरने के लिए कहता है, एक ice cream order में size और flavor की तरह। आपकी choices method क्या करता है यह shape करती हैं।

In this page:

  1. Parameters
Syntax
ruby
def method_name(param1, param2)
  # body
end

method_name(arg1, arg2)

Parameters

Method parameters definition के parentheses में listed होते हैं और method body के अंदर available local variables बन जाते हैं। Arguments default से positionally match होते हैं, call site पर pass होने के order में।

अगर arguments की संख्या method की उम्मीद से match न हो Ruby एक ArgumentError raise करता है (जब तक defaults या splat parameters इस्तेमाल न हों)।

Note: अगर आप required arguments की गलत संख्या के साथ एक method call करें Ruby तुरंत ArgumentError raise करता है।

उदाहरण: Parameters

markup
def greet(name, times)
  times.times { puts "Hello, #{name}!" }
end

greet("Sam", 2)
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. arguments की गलत संख्या pass करना, जो ArgumentError (wrong number of arguments) raise करता है।
  2. arguments को गलत order में pass करना, जैसे greet(2, "Sam")।
  3. यह उम्मीद करना कि एक parameter reassign करके method caller का variable बदल देगा, जब एक parameter reassign करना सिर्फ local copy बदलता है।
🔒

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.