C# Methods, Parameters, and Return Values
Name a reusable operation, pass only the information it needs, return a useful result, and keep Main from becoming the whole program.
Before this lesson
- Variables and types
- Conditions
- Loops
Define and call methods
Use parameters and return types as a contract
Separate calculation from console input and output
The short answer
A method is a named block of code that performs one operation. Parameters are its inputs, a return value is its output, and calling the method transfers control to it before execution returns to the caller.
A method gives an operation a name
As programs grow, Main can become a long sequence in which input, calculations, validation, and output are tangled together. A method creates a boundary around one operation and gives it a name such as CalculateTotal or IsValidAge. The name lets the caller reason at a higher level without rereading every statement.
Calling CalculateTotal transfers control into that method. Its statements run, return sends a value back, and execution continues after the call. The method can be called many times with different arguments without copying its implementation.
Read the signature as a contract
In static decimal CalculateTotal(decimal price, int quantity), the final decimal before the name is the return type. The parameter list states that callers must provide a decimal followed by an int. Inside the method, price and quantity are local names for those supplied values.
A void method returns no value. It is appropriate for an action whose useful effect is elsewhere, such as printing a heading. Calculations are usually more reusable when they return data instead of printing it, because a caller can display, store, compare, or test the result.
static bool IsValidPercentage(decimal value)
{
return value >= 0m && value <= 100m;
}
static void PrintDivider()
{
Console.WriteLine("----------------");
}Separate pure calculation from input and output
A method that receives all needed values and returns a result is easy to understand and test. CalculateDiscount does not need to know whether the subtotal came from a console, file, or web request. It also does not decide how the result is displayed.
This separation is not a rule that every method must be mathematically pure. Some methods must read files or send messages. The point is to keep side effects intentional and avoid hiding unrelated work inside a name that sounds like a calculation.
using System;
class Program
{
static decimal CalculateDiscount(decimal subtotal, bool isMember)
{
decimal rate = isMember ? 0.10m : 0m;
return subtotal * rate;
}
static void Main()
{
decimal subtotal = 80m;
decimal discount = CalculateDiscount(subtotal, true);
Console.WriteLine($"Pay: {subtotal - discount:0.00}");
}
}Expected output
Pay: 72.00
Design parameters around what the method needs
Pass the smallest meaningful inputs. A method that needs price and quantity should not accept five unrelated values ‘in case’ they become useful. A narrow contract makes dependencies visible and reduces the number of states to reason about.
Optional parameters provide a default, and named arguments identify values at the call site. They help when a parameter is genuinely optional or several arguments of the same type are easy to confuse. Too many optional switches often signal that one method is trying to perform several operations.
| Design | Strength | Risk |
|---|---|---|
| Return a value | Caller chooses what to do | Caller must handle it |
| Print inside method | Convenient for display-only action | Harder to reuse and test |
| Several small methods | Names reveal steps | Too much fragmentation if steps have no independent meaning |
| One long method | Everything visible together | Mixed responsibilities become difficult to change |
Good habits
- Use verb phrases for methods: ParseOrder, CalculateTax, PrintReceipt.
- Prefer an early return when it removes deep nesting and the exit condition is clear.
- Do not extract a method only to reduce line count; extract a coherent operation.
Quick knowledge check
Answer before you reveal.
01What is the difference between a parameter and an argument?
A parameter is the named input in a method declaration; an argument is the value supplied at a particular call.
02Why might CalculateTax returning decimal be preferable to printing it?
The caller can reuse the numeric result for totals, storage, tests, or different output formats.
Practice challenge
Now build it without copying.
Create a CalculateShipping method that accepts an order total and whether delivery is express. Standard delivery costs 5 but is free from 50; express adds 8 to the standard cost. Call it with at least three boundary cases.
You are done when
- The method prints nothing and returns a decimal
- Parameter names communicate their meaning
- Calls include totals below, at, and above the free-delivery boundary
Stretch: Extract a separate IsFreeStandardShipping method and decide whether it makes the rule clearer.
Open challenge in playgroundLesson checkpoint
One small step locks it in
Mark this lesson complete, then keep the momentum going.
Clear up the details
Frequently asked questions
Can two methods have the same name?
Yes, when their parameter lists differ meaningfully; this is overloading. Avoid overloads that make calls ambiguous or give the same name unrelated meanings.
What does static mean here?
A static method belongs to its type rather than a particular object. Main is static, so these early helper methods are static too. Instance methods arrive with classes and objects.
Should every method be short?
A method should be focused and readable. Line count is a signal, not a law; a clear 25-line algorithm can be better than five disconnected tiny methods.