Step 1 of 6 · Reading · ~3 min
Learn
Methods
Defining Methods
By the end of this lesson you can give a computation a name, pass values into it, get a result back, and read any method signature left to right.
A method is a named block of code you can run from anywhere. In C# every method lives inside a class, and its signature tells you how to call it:
static int Clamp(int value, int low, int high) { ... }
static- belongs to the class itself, not to an object.Mainis static, and a static method can call only other static methods directly. Forgetstaticon a helper thatMaincalls and the compiler says "an object reference is required".int- the return type, written before the name.voidmeans "returns nothing".Clamp- the name. The convention is PascalCase.(int value, int low, int high)- the parameters, each with its own type.
Inside a method, return hands back a value and ends the method immediately. Here is a complete program with two helpers:
using System;
class Program {
static int Clamp(int value, int low, int high) {
if (value < low) return low;
if (value > high) return high;
return value;
}
static void PrintBanner(string text) {
Console.WriteLine("== " + text + " ==");
}
static void Main() {
PrintBanner("scores"); // == scores ==
Console.WriteLine(Clamp(120, 0, 100)); // 100
Console.WriteLine(Clamp(-4, 0, 100)); // 0
}
}
Every path through a method that has a return type must end in a return; the compiler checks this ("not all code paths return a value"). Main shrinks to pure intent: read input, call methods, print. That separation is what makes code testable.
Arguments are copies
When you call a method, each argument is copied into its parameter. Changing the parameter inside the method does not change the caller's variable:
static void AddTen(int x) { x = x + 10; }
int a = 5;
AddTen(a);
Console.WriteLine(a); // still 5
If you want a result, return it and assign it: a = AddTen(a); with a method that returns the new value.
Overloading and shorthand
Overloading means the same name with different parameter types; the compiler picks one from the arguments. A default parameter lets callers omit a trailing argument. A method whose body is one expression can use =>:
static int Twice(int n) => n * 2;
static double Twice(double n) => n * 2;
static string Repeat(string s, int times = 2) { /* ... */ } // Repeat("ab") uses times = 2
The trap: a keyword in disguise
Some natural parameter names are reserved words in C#. base refers to a parent class, so this does not compile:
static long Power(int base, int exp) { ... } // syntax error at 'base'
That is why the starter names its parameters b and e. Keep them.
Common mistakes
- Forgetting
staticon a helper called fromMain: "an object reference is required". - Printing instead of returning. A method that prints its answer cannot be reused, and
Mainprints the result as well, so the line appears twice. - Expecting a parameter change to reach the caller. The argument was copied.
- A return type that is too small. A method that returns
intwraps around silently once the result passes about 2.1 billion; uselongwhen results grow.
Your exercise
The starter has the method signature and a Main that calls it; the body is a placeholder that always returns 0. Fill it in so it returns the value described in the exercise. The most likely mistake is a loop that runs one pass too many or too few, so check what your method returns when the exponent is 0.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…