Start learning free

LESSON 30 OF 347 · FUNCTIONS I

Statement Order in a C# Method: Fixing the Buggy Code

Practice Read the full explanation

Inside the code editor we've tried to define and call a function that would print the messages Hey coder! then Welcome back! to the console.

So when we ran the code, we expected it to print:

Hey coder!
Welcome back!

but it seems like we made some mistakes because when we run our code we get:

Welcome back!
Hey coder!

Assignment:

Your task is to fix our code such that it will print the desired output.

STEP 1 OF 1
    Ctrl + Enter to check

    C# · FUNCTIONS I

    Buggy Code Call Before Definition in C#, explained

    What the program should do

    The program should print two lines, in this order:

    Hey coder!
    Welcome back!
    

    When it runs, the lines come out the wrong way round: Welcome back! first, then Hey coder!. Every line of the right text is printed, so nothing is missing. Only the order is wrong, and the task is to fix it.

    Here is the starting code:

    public void WelcomeUser() {
        Console.WriteLine("Welcome back!");
        Console.WriteLine("Hey coder!");
    }
    
    public void MainFunction() {
        WelcomeUser();
    }
    

    Finding the cause

    Start by tracing what happens when the program runs. MainFunction has one statement, a call to WelcomeUser(). That call runs the body of WelcomeUser from top to bottom. The first statement prints Welcome back!, the second prints Hey coder!, and so the output is in the wrong order.

    So the call isn't the problem. It happens once, at the right time. The order of the output is decided entirely inside WelcomeUser, because statements in a method body run in exactly the order you write them. C# doesn't reorder them or guess what you meant.

    The fix

    Swap the two Console.WriteLine lines so that the greeting comes first:

    public void WelcomeUser() {
        Console.WriteLine("Hey coder!");
        Console.WriteLine("Welcome back!");
    }
    
    public void MainFunction() {
        WelcomeUser();
    }
    
    /* Explanation:
    
    Remember, the statements inside a function are executed in the exact order we write them.
    That's why "Welcome back!" gets printed before "Hey coder!" in the console.
    We have to swap the "Console.WriteLine()" statements' order inside our function.
    
    */
    

    Nothing else changes. The call stays the same, because the call was never the issue.

    Does the order of the definitions matter?

    This lesson's title suggests a call coming before a definition, so it's worth separating two ideas that beginners often mix up.

    • The order of statements inside a body matters. They run top to bottom, one after another, and that's what this bug is about.
    • The order of method definitions in a class doesn't matter. If MainFunction were written above WelcomeUser in the class, the program would behave the same, because the compiler reads the whole class before it runs anything.

    So when output looks wrong, don't ask where the methods are placed. Ask which statements ran, and in what order.

    A trace to check yourself

    After the fix, the run goes like this:

    1. MainFunction starts and reaches the call WelcomeUser();.
    2. Control jumps into WelcomeUser, which prints Hey coder!.
    3. The next statement prints Welcome back!.
    4. The method ends, control returns to MainFunction, which has nothing more to do.

    Writing a trace like this on paper is the quickest way to find an ordering bug, because it shows exactly which line produced which output.

    Why the compiler doesn't complain

    This program compiles and runs without any error message. The compiler checks that your code follows the rules of C#, such as matching braces and valid names. It can't know what you meant the output to be. Both orders of the two print lines are valid C#, so it accepts either one. A bug like this, where the program runs but does the wrong thing, is called a logic error, and finding it is your job.

    The way to find one is to compare what you expected with what you got, and then work backwards. The output has the right lines in the wrong order, which points straight at the code that prints them and the order it prints in. When the cause isn't obvious, you can add a temporary Console.WriteLine at the start of each method to see which one runs first.

    The same bug in another shape

    The mistake isn't limited to two print lines in one method. The same wrong order can happen between calls. Suppose the program had two methods and called them in the wrong order:

    void Hey() {
        Console.WriteLine("Hey coder!");
    }
    
    void Welcome() {
        Console.WriteLine("Welcome back!");
    }
    
    void MainFunction() {
        Welcome();
        Hey();
    }
    

    Running MainFunction prints Welcome back! first and Hey coder! second, which is the same wrong result. This time the fix is in MainFunction: swap the two calls so Hey() comes first. The question to ask is the same each time. Which body decides the order of these lines? Find that body and put its statements in the order you want the output to appear.

    If you changed the fix so the strings were different, the output would differ too. Keep the text exactly as given, including the capital letters and the exclamation mark.

    Testing the fix

    With the fixed WelcomeUser, running MainFunction prints exactly two lines, Hey coder! then Welcome back!, each followed by a line break because Console.WriteLine ends the line after printing. If your output still shows the lines reversed, check that you swapped the statements in WelcomeUser and that the edit was saved.

    Common mistakes

    • Editing the output text instead of the order. The strings are already correct; changing them would pass the eye but not the order.
    • Adding a second call to WelcomeUser(), which would print the pair twice.
    • Moving the whole method instead of swapping its two lines. That does nothing, as explained above.
    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