Start learning free

LESSON 28 OF 347 · FUNCTIONS I

Defining a Method in C#: Fixing the Buggy Code

Practice Read the full explanation

Inside the code editor we've tried just to define a function (without calling the function) that would print the message Hey, Andy! whenever called.

It seems like we made some mistakes because when we run our code we get an error.


Assignment:

Your task is to fix our function definition such that no errors will be produced.

STEP 1 OF 1
    Ctrl + Enter to check

    C# · FUNCTIONS I

    Buggy Code Defining A Function in C#, explained

    What the code should do

    The goal is to define a method named SayMyName that prints Hey, Andy! whenever it is called. The task asks only for the definition. Nothing calls the method, so a correct answer compiles and prints nothing when it runs.

    The starting code looks nearly right, but the compiler rejects it:

    public void SayMyName()
    Console.WriteLine("Hey, Andy!");
    

    What goes wrong

    A person reading these two lines assumes the second belongs to the first. The compiler doesn't make that guess. It reads public void SayMyName() as the header of a method, and the next thing it expects is either a body in curly braces or, for a method with no body, a semicolon. It finds neither, and it reports errors such as ; expected.

    The second line causes a problem of its own. Console.WriteLine("Hey, Andy!"); is a statement, and a statement can only live inside the body of a method (or another place where code runs). Directly inside a class there is room for declarations like methods and fields, not for calls.

    The root cause is a single missing piece: the method has no body, so nothing ties the print line to SayMyName.

    The fix

    A method body is a block, and a block is always wrapped in curly braces. The opening brace goes after the header and the closing brace after the last statement:

    public void SayMyName() {
        Console.WriteLine("Hey, Andy!");
    }
    

    Now the compiler can see exactly what SayMyName contains. Each part of the definition has a job:

    • public lets other code use the method.
    • void says it returns no value.
    • SayMyName is the name, in PascalCase.
    • () shows it takes no inputs.
    • The braces enclose the body, and everything inside them runs when the method is called.

    Indenting the print line is optional for the compiler, but it makes it clear at a glance which statements belong to the method.

    Checking your fix

    After the fix the code should compile without errors, and running it shows no output at all. That's expected: the method is defined but never called. To see the greeting you would add a call somewhere that runs, SayMyName();, and it would print Hey, Andy! once for each call.

    How to spot this kind of bug

    When code won't compile, read the first error message and look at the line it points to, then at the line just before it. Here the compiler is confused about the header line, but the real cause is that the body is missing. A useful habit is to check, for every method, that you can point at its opening brace, its statements and its closing brace. If you can't, the compiler can't either.

    It also helps to remember that a compile error stops everything. A program that doesn't compile never starts, so none of its code runs, not even lines that look fine on their own.

    Related mistakes

    • A semicolon after the header. Writing public void SayMyName(); ends the header with a semicolon, which isn't valid for an ordinary method with a body.
    • A missing closing brace. Every { needs a matching }. A forgotten one usually produces an error near the end of the file, far from the real mistake.
    • A missing semicolon after the print call. Statements end with ;, but the closing brace of a method does not.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free