Start learning free

LESSON 28 OF 338 · FUNCTIONS I

Defining a Function in Go: Fixing the Buggy Code

Practice Read the full explanation

Inside the code editor we've tried to define a function named sayMyName that prints Hey, Andy!.

A Go function body must be wrapped in curly braces:

func sayMyName() {
    fmt.Println("Hey, Andy!")
}

Assignment:

Your task is to fix the function so the code is valid Go and the message is printed when the test calls it.


STEP 1 OF 1
    Ctrl + Enter to check

    GO · FUNCTIONS I

    Buggy Code Defining A Function in Go, explained

    What the code is supposed to do

    The goal is to define a function named sayMyName that prints Hey, Andy! every time it is called. The task asks only for the definition, not for a call. The code you start with doesn't compile:

    func sayMyName()
    fmt.Println("Hey, Andy!")
    

    The compiler stops with this message:

    syntax error: non-declaration statement outside function body
    

    What goes wrong

    A person reading the two lines assumes the second belongs to the first. The compiler does not guess. It reads func sayMyName() as a complete header with no body, and then meets fmt.Println("Hey, Andy!") as a separate statement sitting directly at the top level of the file.

    At the top level of a Go file, only declarations are allowed: func, var, const, type and import. A call such as fmt.Println(...) is a statement, and statements may only appear inside a function body. That is what the error means: a non-declaration statement outside a function body.

    Why the braces matter

    A function definition has these parts:

    • the keyword func;
    • the name, here sayMyName;
    • parentheses for the parameters, empty here;
    • an optional return type, left out because the function returns nothing;
    • the body, wrapped in { and }.

    The braces are the only thing that marks where the body starts and ends. Go does not use indentation for this, so indenting the Println line under the header would look right to a reader and change nothing for the compiler.

    Fixing it step by step

    1. Keep the header. func sayMyName() already has the keyword, the exact name the task asks for and empty parentheses.
    2. Open the body on the same line. Add { after the closing parenthesis. Go ends a line with an automatic semicolon after ), so a brace on the next line fails with unexpected semicolon or newline before {.
    3. Keep the print statement between the braces, indented by four spaces so readers can see it belongs to the function.
    4. Close the body with } on its own line, lined up with func.
    5. Add no call. The task says to define the function, not to run it.

    The fixed code in Go

    func sayMyName() {
        fmt.Println("Hey, Andy!")
    }
    

    Some Go details around these three lines:

    • No return type means no result. The function returns nothing; it only prints.
    • Println adds a newline. It prints its argument and moves to the next line. The capital P is required, because only capitalized names are exported from the fmt package.
    • The text is exact. The comma, the space and the exclamation mark are part of the string. "Hey Andy!" prints something different.
    • No semicolons to write. Go inserts them at line ends for you.
    • Imports. A complete program that uses fmt must also import it, and Go rejects an import that nothing uses.

    Defining is not running

    Once the definition compiles, nothing has been printed. The body runs only when the function is called. In a complete program that call goes in main, where Go starts running:

    package main
    
    import "fmt"
    
    func sayMyName() {
        fmt.Println("Hey, Andy!")
    }
    
    func main() {
        sayMyName()
        sayMyName()
    }
    

    This prints Hey, Andy! twice, once per call. For this lesson, the definition alone is the answer.

    Common mistakes

    • Indenting instead of adding braces. The error stays the same.
    • Putting { on the next line. Go reports a syntax error before the brace.
    • Leaving out the closing brace. The compiler reaches the end of the file inside the body and reports a syntax error.
    • Using empty braces. func sayMyName() {} followed by the print line gives the function an empty body and leaves the statement outside it, so the original error remains.
    • Changing the name. Names are case-sensitive: SayMyName is a different, exported function, not sayMyName.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free