Codeloft
0 0 XP

Methods

7 min 20 XP + 10 for no hints

In C#, functions are called methods. A method has a return type, a name and parameters: static int Square(int n) { return n * n; }

static lets Main call the method without creating an object first; you'll see why in the classes lesson. void means a method returns nothing.

By convention, C# method names start with a capital letter: Square, PrintReceipt.

Example
static string Greet(string name)
{
    return $"Hello, {name}!";
}

static void Main()
{
    Console.WriteLine(Greet("Ada"));
}
Output
Hello, Ada!
Methods live inside the class, next to Main, not inside Main.

Your tasks

0 of 3 done
  1. Define static int Area(int width, int height)
  2. Return width multiplied by height
  3. Call Area a second time with other numbers

Quick check

Why is Area marked static here?

Program.csChecked as you type
No problems found. Unclosed tags, missing semicolons and errors show up here in plain words.