Declaring and Calling Methods
A method is a named block of code with a return type, a parameter list and a body; calling it runs the body with the values you supply.
returnType MethodName(type parameter1, type parameter2) { ... }A method is a named block of code. You write it once and run it whenever you need it. Running it is called calling the method. Without methods, every program would be one long list of statements with no way to reuse any part of it.
The four parts of a declaration
A declaration is written in this order: the return type, the name, the parameter list in round brackets, then the body in curly braces. The return type says what kind of value comes back out. The word void in that position means no value comes back; the method just does something, such as printing.
Parameters and arguments
A parameter is a variable that lives inside the method and receives a value from the caller. The value the caller supplies is an argument. They are matched by position: the first argument goes to the first parameter, the second to the second. The names used by the caller are irrelevant.
Returning
The return statement hands a value back and ends the method immediately; nothing after it runs. In a void method you may write return; on its own just to stop early.
What trips people up
A method with two parameters of the same type will happily accept them in the wrong order, and nothing complains until the answer is wrong. A non-void method must return a value on every possible path, or the compiler reports CS0161. And a returned value you never use is silently thrown away.
Example
// A method declaration: return type, name, parameter list, body.
int Difference(int from, int take)
{
return from - take;
}
// void means this method hands no value back. It only prints.
void Report(string label, int value)
{
Console.WriteLine($"{label}: {value}");
}
// Arguments are matched to parameters by position, so order matters.
Report("10 take 3", Difference(10, 3));
Report("3 take 10", Difference(3, 10));
// The returned value can be stored in a variable first.
int kept = Difference(100, 58);
Report("kept", kept);
// Output:
// 10 take 3: 7
// 3 take 10: -7
// kept: 42When to use it
- Pull a calculation that appears in three places into one named method.
- Give a block of code a name so the surrounding code reads as a list of steps.
- Write a void method that prints a formatted report line, so the formatting lives in one place.
More examples
Leaving a void method early
A bare return in a void method stops it at that point. This is the usual way to handle a bad input first and keep the rest of the body free of nesting.
void Describe(int score)
{
if (score < 0)
{
Console.WriteLine("score: not set");
return; // ends the method here; the lines below do not run
}
Console.WriteLine($"score: {score}");
}
Describe(-1);
Describe(88);
// Output:
// score: not set
// score: 88One method calling another
A method call is just an expression that produces a value, so calls can be used inside other methods. Reach for this whenever the same small calculation appears in two places.
int Square(int n)
{
return n * n;
}
// This method does not repeat the multiplication. It calls Square twice.
int SumOfSquares(int a, int b)
{
return Square(a) + Square(b);
}
Console.WriteLine(Square(5));
Console.WriteLine(SumOfSquares(3, 4));
// Output:
// 25
// 25No parameters, and a value back
A method does not need parameters to be useful; it can return a fixed or computed value. Naming a value this way is often clearer than repeating the literal in several places.
// Empty brackets mean no parameters. The brackets are still required.
string Banner()
{
return "== report ==";
}
int LineCount(string text)
{
return text.Split('\n').Length;
}
Console.WriteLine(Banner());
Console.WriteLine(LineCount("one\ntwo\nthree"));
// Output:
// == report ==
// 3
Discussion