Parameters कैसे काम करते हैं
Parameters वे blanks हैं जिन्हें method आपसे भरने के लिए कहता है, जैसे किसी pizza place को अपना size और toppings बताना। method आपके जवाबों का उपयोग अपना काम करने के लिए करता है।
In this page:
Syntax
static void MethodName(type parameter1, type parameter2)
{
// body
}
MethodName(argument1, argument2);
Parameters
Parameters किसी method को अपने caller से input values पाने देते हैं, हर एक एक declared type और नाम के साथ। Arguments default रूप से value types के लिए value से पास होते हैं, यानी method को अपनी खुद की copy मिलती है।
कई parameters declaration और call दोनों में commas से अलग किए जाते हैं।
Note:
Parameter names सिर्फ उनका type नहीं, उनका purpose describe करने चाहिए।
उदाहरण: Parameters
using System;
class Program
{
static void Greet(string name, int times)
{
for (int i = 0; i < times; i++)
{
Console.WriteLine($"Hello, {name}!");
}
}
static void Main(string[] args)
{
Greet("Sam", 3);
}
}
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. #}
आम गलतियां
- एक जैसे types होने पर arguments को गलत order में पास करना, जो compile होता है लेकिन गलत results देता है।
- यह उम्मीद करना कि कोई method caller का int बदल देगा, जबकि value-type parameters copies हैं।
- बहुत कम या ज़्यादा arguments पास करना, जो एक compile error है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: