Methods

By the end of this lesson you'll be able to package code into reusable, named methods — passing in parameters , handing back results with return , giving parameters sensible defaults, and overloading one name for several jobs. This is how real programs stay organised instead of becoming one giant block of code.

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

Part of the free C# course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

A method is like a kitchen appliance . A blender takes ingredients ( parameters ), processes them, and hands back a smoothie (the return value ) — and you never have to know how the motor works inside (that's abstraction ). The label on the button is the method name ; what you drop in is the arguments . Overloading is a multi-function cooker: one name on the front, but it bakes, steams, or pressure-cooks depending on which settings (parameter types) you give it. Once you've built an appliance, you press its button as many times as you like — that's the whole point of a method.

Parameter = the placeholder named in the method's definition. Argument = the actual value you pass in when you call it. Same slot, two words — they trip everyone up at first.

1. Defining & Calling Methods

A method has a return type (what it gives back), a name , and a list of parameters (its inputs). Use void when it just does something with no result to report; use a type like int , string or double when it should hand a value back with return . Simple one-liners can use the expression-bodied form instead of {' return ...; '} . Read this worked example first, run it, then you'll finish one yourself.

Your turn. The method below is meant to return the area of a rectangle, but the return type and the return expression are blank. Fill in the two ___ blanks using the hints, then run it.

2. Parameters — Optional, Named, Params & Out

Optional parameters carry a default value, so callers can leave them off (they must come after all required parameters). Named arguments let you pass values by name in any order, which makes calls self-documenting. params accepts any number of arguments as an array. And out lets a method hand back more than one value at once — it's exactly how int.TryParse returns both a success flag and the parsed number.

Now you try. The method below greets someone, with a greeting that should default to "Hello". Fill in the default value and the two calls — one using the default, one overriding it.

3. Overloading, Scope & Recursion

Method overloading means giving several methods the same name but different parameter types or counts — the compiler picks the right one from the arguments you pass (note: the return type alone can't tell two overloads apart; the parameters must differ). Scope is where a name is visible: a variable declared inside a method is local to it and disappears when the method ends. Recursion is a method that calls itself — handy for problems that shrink toward a base case that stops the recursion.

By default a method gets a copy of the value you pass (this is "pass by value"). Changing the parameter inside the method does not change the caller's variable. To let a method change the caller's variable, use ref (in and out) or out (out only — the method must assign it before returning).

Use out when a method needs to produce an extra result (like TryParse ). Use ref when it needs to read and update an existing value. Most of the time, prefer returning a value (or a tuple) over ref — it's clearer.

Here's a small program that uses everything from this lesson at once — a params method to total any number of prices, an expression-bodied method with an optional tax rate, a void printer, and a named argument to override the default. You understand every line now.

Notice how each method does one job and has a name that says what it does. That's the habit to build: small, well-named methods read like a description of the program.

Q: What's the difference between a parameter and an argument?

A parameter is the named placeholder in the method's definition — int a in Add(int a, int b) . An argument is the real value you pass when you call it — the 5 in Add(5, 3) . Definition side = parameter; call side = argument.

Use void when the method's job is to do something (print, save, send) and there's no answer to hand back. Use a return type when the caller needs a result — a total, a grade, a true/false. If you find yourself printing inside a method and the caller also wants the value, return it instead and let the caller print.

Q: What's the difference between out and ref ?

Both let a method change the caller's variable. out is "output only" — the method must assign it before returning, and the caller doesn't need to set it first (used by TryParse ). ref is "in and out" — the value must be set before the call and the method can read and change it.

Q: Why won't C# let me overload by return type?

When you write Multiply(2, 3) , the compiler chooses the overload from the arguments , not from what you do with the result. Two methods that differ only in return type would be ambiguous, so C# requires the parameter lists to differ.

No blanks this time — just a brief and an outline. Write a method static int Max(int a, int b) that returns the larger of its two arguments, then call it on a few pairs in Main and print the results. Run it and check your output against the expected lines in the comments.

Practice quiz

What return type does a method use when it does something but returns no value?

  • null
  • empty
  • void
  • int

Answer: void. void means the method performs an action and hands nothing back.

What is the difference between a parameter and an argument?

  • A parameter is the placeholder in the definition; an argument is the value passed at the call
  • They are the same thing
  • A parameter is the value passed; an argument is the placeholder
  • Arguments only exist for void methods

Answer: A parameter is the placeholder in the definition; an argument is the value passed at the call. Definition side = parameter; call side = argument.

What does the expression-bodied syntax => expr replace?

  • A loop
  • A constructor
  • An if statement
  • A { return expr; } body

Answer: A { return expr; } body. => is shorthand for a one-line method body that returns the expression.

Where must optional (default-valued) parameters appear in the parameter list?

  • First
  • After all required parameters
  • Anywhere
  • Only alone

Answer: After all required parameters. Optional parameters must come after all required ones, or the code won't compile (CS1737).

What does the params keyword allow?

  • Accepting any number of arguments as an array
  • Returning multiple values
  • Naming arguments
  • Default values

Answer: Accepting any number of arguments as an array. params lets a method accept a variable number of arguments collected into an array.

Which keyword lets a method hand back an extra value, as int.TryParse does?

  • ref
  • params
  • out
  • static

Answer: out. out lets a method produce an extra output; it must be assigned before the method returns.

What makes two methods valid overloads of the same name?

  • Different return types only
  • Different parameter lists (type or count)
  • Different names
  • Being in different files

Answer: Different parameter lists (type or count). Overloads must differ in their parameters; the return type alone cannot distinguish them.

What must a recursive method have to avoid running forever?

  • A loop
  • An out parameter
  • A static field
  • A base case that stops the recursion

Answer: A base case that stops the recursion. Recursion needs a base case (e.g. n <= 1) that returns without recursing further.

By default, passing an int to a method passes it how?

  • By reference
  • By value (a copy)
  • As an out parameter
  • It cannot be passed

Answer: By value (a copy). Value types are passed by value — the method gets a copy, so changes don't affect the caller's variable.

What does a named argument like F(times: 2, msg: "Hi") allow?

  • Skipping the method body
  • Returning two values
  • Passing arguments by name in any order
  • Making the method static

Answer: Passing arguments by name in any order. Named arguments let you pass values by parameter name in any order, making calls self-documenting.