Reading — step 1 of 6
Learn
Defining Methods
A method gives a name to a computation so you can run it from anywhere, test it in isolation, and read Power(2, 10) instead of ten lines of loop. In C#, every method lives inside a class — there are no free-floating functions — and the signature tells you everything about how to call it:
static long Power(int b, int e) { ... }
Reading left to right:
static— belongs to the class itself, not to an object.Mainis static, and a static method can only call other static methods directly — so helpers you call fromMainmust be static too. Forget it and the compiler says "an object reference is required".long— the return type, written before the name. Usevoidfor methods that return nothing.Power— the name; C# convention is PascalCase for methods.(int b, int e)— the parameters, each with its own type.
Inside a method, return hands back a value and exits immediately. Here is the accumulator pattern in method form — summing the numbers 1 through n:
static long SumTo(int n) {
long total = 0;
for (int i = 1; i <= n; i++) {
total += i;
}
return total;
}
static void Main() {
Console.WriteLine(SumTo(10)); // 55
}
Once a computation has a name, Main shrinks to pure intent: read input, call the method, print the result. That separation — logic in methods, orchestration in Main — is how real C# programs are shaped, and it is what makes code testable.
Extras you will meet constantly
Expression-bodied methods — a one-expression method can drop the braces (C# 6+):
static int Square(int n) => n * n;
Overloading — same name, different parameter types; the compiler picks the right one from the argument types:
static int Add(int a, int b) => a + b;
static double Add(double a, double b) => a + b;
Default parameters — callers may omit trailing arguments:
static string Greet(string name, string greeting = "Hello")
=> greeting + ", " + name + "!";
The trap: a keyword in disguise
A power function naturally wants parameters called "base" and "exp" — but base is a reserved keyword in C# (it refers to a parent class, as you will see in the inheritance lesson):
// Trap: does not compile
static long Power(int base, int exp) { ... } // syntax error at 'base'
That is why the starter names the parameters b and e. Keep those names.
Your exercise
Implement Power(int b, int e): return b raised to the power e using a loop (e is always >= 0). It is the same accumulator pattern as SumTo, with two changes: seed long result = 1; (not 0 — you are multiplying now), then multiply by b exactly e times — for (int i = 0; i < e; i++) result *= b;. The starter's Main already reads the two integers and prints Power(b, e); you only fill in the method body.
The grader will catch three specific mistakes: leaving the starter's placeholder return 0; in place (every test fails); seeding the accumulator with 0 (everything multiplies to 0); and looping with i <= e — one multiplication too many, so input 2 10 prints 2048 instead of 1024. Input 5 0 expects 1: seed 1, zero loop iterations, return it untouched.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…