Methods: Naming a Piece of Work
By the end of this lesson
Extract a block of code into a named method with inputs and a result, and explain why that helps.
A method is a named piece of work. You give it inputs, it does something, and usually it hands back a result.
Other languages call this a function. The idea is identical: take a chunk of code, give it a name, and refer to it by that name instead of repeating it.
decimal price1 = 100m;
decimal tax1 = price1 * 0.18m;
decimal total1 = price1 + tax1;
decimal price2 = 250m;
decimal tax2 = price2 * 0.18m;
decimal total2 = price2 + tax2;
decimal price3 = 75m;
decimal tax3 = price3 * 0.18m;
decimal total3 = price3 + tax3;This works, and it is a problem. The tax rate appears three times, so changing it means finding all three. Miss one and you have a bug that only shows up for certain orders.
decimal CalculateTotalWithTax(decimal price)
{
decimal taxRate = 0.18m;
decimal tax = price * taxRate;
return price + tax;
}
decimal total1 = CalculateTotalWithTax(100m);
decimal total2 = CalculateTotalWithTax(250m);
decimal total3 = CalculateTotalWithTax(75m);- decimal at the start states what kind of value comes back.
- CalculateTotalWithTax is the name. It describes what the method produces.
- (decimal price) is the input, called a parameter. Each call supplies its own value.
- return hands the result back to whoever called it.
Methods that return nothing
void PrintReceipt(string customer, decimal total)
{
Console.WriteLine("----------------------");
Console.WriteLine($"Customer: {customer}");
Console.WriteLine($"Total: {total}");
Console.WriteLine("----------------------");
}
PrintReceipt("Priya", 118m);- void means this method produces no value — it is called for what it does, not for what it gives back.
- It takes two parameters. Their order matters at the call site.
Signs a block of code wants to become a method:
- You have written something very similar more than twice
- You want to explain a chunk of code with a comment — the method name can be that explanation
- A single stretch of code does several distinct things in sequence
- You are struggling to name a variable because it holds an intermediate value nobody outside cares about
Summary
- A method gives a name to a piece of work, with inputs and usually a result
- Extracting repeated logic means a change happens in one place instead of several
- void means the method returns nothing and is called for its effect
- If a method name needs "and", it is likely doing too much
Practice
Attempt each one before opening the solution. Getting it wrong first is how the idea sticks.
Try it yourself
Try it yourself
Write a method that takes a distance in kilometres and a rate per kilometre and returns the fare, including a fixed 2.50 booking fee.
Call it three times with different distances.
Show solution
The booking fee lives inside the method because it is part of how a fare is calculated, and keeping it there means it is defined once.
decimal CalculateFare(decimal distanceKm, decimal ratePerKm)
{
decimal bookingFee = 2.50m;
return bookingFee + (distanceKm * ratePerKm);
}
Console.WriteLine(CalculateFare(8m, 1.20m)); // 12.10
Console.WriteLine(CalculateFare(2.5m, 1.20m)); // 5.50
Console.WriteLine(CalculateFare(14m, 1.05m)); // 17.20Think about it
Think about it
In the fare example, the booking fee is fixed inside the method. When would that be the right decision, and when would it be better as a parameter?
Show solution
Keeping it inside is right while there is one fee that is genuinely part of the definition of a fare. It keeps the call simple.
It should become a parameter as soon as the fee varies — by city, by time of day, by vehicle type. The signal is a second caller wanting a different value, at which point hard-coding it forces either a duplicate method or a special case.
This is a recurring judgement: keep it simple until a real second case appears, rather than adding flexibility in advance for one you imagine.
Knowledge check
Nothing is recorded and there is no score. The explanation appears either way.
Saved in this browser only.