Parameters
Parameters are the blanks a method asks you to fill in, like telling a pizza place your size and toppings. The method uses your answers to do its job.
In this page:
Syntax
static void MethodName(type parameter1, type parameter2)
{
// body
}
MethodName(argument1, argument2);
Parameters
Parameters let a method receive input values from its caller, each with a declared type and name. Arguments are passed by value for value types by default, meaning the method gets its own copy.
Multiple parameters are separated by commas in both the declaration and the call.
Note:
Parameter names should describe their purpose, not just their type.
Example: 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. #}
Common Mistakes
- Passing arguments in the wrong order when the types are the same, which compiles but gives wrong results.
- Expecting a method to change the caller's int, when value-type parameters are copies.
- Passing too few or too many arguments, which is a compile error.
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: