Lesson 9 of 34 · Methods
C# Methods: Parameters and Return Values
Welcome to the second wing of the Citadel. So far every program has been one long list of statements. Real programs are built from methods: named, reusable blocks of code that take inputs and hand back a result. Methods let you solve a problem once, give the solution a good name and use it everywhere.
Anatomy of a method
public static int Square(int x)
{
return x * x;
}
Reading the first line from left to right:
public— the method can be used from outside its class (the hidden tests need this).static— the method belongs to the class itself, not to an object. Chapter 4 explains this properly; until then, all our methods arestatic.int— the return type: the kind of value the method hands back.Square— the method's name. Method names use PascalCase:CalculateDamage,IsLeapYear.(int x)— the parameter list. Each parameter has a type and a name and behaves like a variable inside the method.
return ends the method and sends a value back to the caller:
int area = Square(5); // 25
Console.WriteLine(Square(3) + 1); // 10
The values you pass in (5, 3) are called arguments. They are matched to the parameters by position.
Several parameters, and void
Separate parameters with commas. A method that does something but returns nothing has the return type void:
public static double Distance(double x1, double y1, double x2, double y2)
{
double dx = x2 - x1;
double dy = y2 - y1;
return Math.Sqrt(dx * dx + dy * dy);
}
public static void Announce(string hero, int level)
{
Console.WriteLine($"{hero} has reached level {level}!");
}
Expression-bodied methods
When a method only returns one expression, you can write it with =>:
public static int Double(int x) => x * 2;
public static bool IsAdult(int age) => age >= 18;
Methods calling methods
Methods are building blocks. Small methods with clear names make bigger ones easy to read:
public static bool IsEven(int n) => n % 2 == 0;
public static string Parity(int n)
{
return IsEven(n) ? "even" : "odd";
}
Every path must return
If a method promises a return type, every way through it must end in return. This does not compile, because nothing is returned when n is negative:
public static string Sign(int n)
{
if (n >= 0)
{
return "plus";
}
} // error CS0161: not all code paths return a value
Common mistakes
- Ignoring the result:
Square(4);on its own computes 16 and throws it away. Store it or use it. - Printing instead of returning: if a method should return a value,
Console.WriteLineinside it is not enough — the tests read the returned value. - Wrong argument order:
Distance(x1, x2, y1, y2)compiles but computes nonsense. Name parameters clearly.
Assignment
Complete five methods in Program:
Square(x)returnsx * x.Hypotenuse(a, b)returns the length of the long side of a right triangle: √(a² + b²).Hypotenuse(3, 4)is5.IsLeapYear(year)— divisible by 4, except years divisible by 100, unless they are also divisible by 400 (2000 was a leap year, 1900 was not).PrintBanner(title)prints=== <title> ===and returns nothing.Describe(year)returns"2024 is a leap year"or"2023 is not a leap year"— reuseIsLeapYear.
Premium lesson
Unlock the full Learn C# course
- All 34 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Functions and methods in other languages
- JavaScript Functions: Parameters and Return Values · JavaScript
- Python Functions: def, Parameters and return · Python
- Go Functions: Parameters and Return Values · Go
- Java Methods: Parameters and Return Values · Java
Need a quick reminder? The C# cheat sheet has every topic of Learn C# on one page.
Lesson last updated on .