Start learning free

LESSON 29 OF 415 · FUNCTIONS I

Defining a Function in Swift: 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

    SWIFT · FUNCTIONS I

    Buggy Code Defining A Function in Swift, explained

    What the code is supposed to do

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

    The code you start with doesn't compile:

    func sayMyName()
    print("Hey, Andy!")
    

    Your job is to find out why and fix the definition.

    What goes wrong

    A reader sees two lines and assumes the second one belongs to the first. The compiler doesn't assume anything. It reads func sayMyName() as a function header and expects a body, which in Swift is always a block in curly braces. Instead it finds print(...), and it reports:

    error: expected '{' in body of function declaration
    

    Two things are wrong at once:

    1. The function has no body. A function declaration in Swift needs { ... } after its header. Without the braces, the header is incomplete.
    2. The print line is not part of the function. Only the braces tell the compiler which statements belong to a function. Indentation, line breaks or the order of lines don't count. Indenting print under the header would look right to a person and still fail. Even once the header is fixed, a print line outside the braces is ordinary top-level code that runs by itself, not when the function is called.

    The program fails to compile, so it never starts.

    Fixing it step by step

    1. Keep the header. func sayMyName() is correct: the func keyword, the exact name the task asks for and empty parentheses.
    2. Open the body. Add { after the header, on the same line.
    3. Move the statement inside. The print line goes between the braces, indented by four spaces so readers can see it belongs to the function.
    4. Close the body. Put } on its own line below the statement.
    5. Don't add a call. The task is to define the function, not to run it.

    The fixed code in Swift

    func sayMyName() {
        print("Hey, Andy!")
    }
    

    A few details about these three lines:

    • No return type. Without -> Type after the parentheses, the function returns nothing. Its only effect is the printed line.
    • print ends the line. It writes its text and then a new line.
    • The text must match exactly. The comma, the space and the exclamation mark are part of the output. "Hey Andy" would not pass.
    • No semicolons. Swift ends a statement at the end of the line.

    Defining is not running

    Once the definition compiles, nothing is printed yet. The body runs only when something calls the function, by writing its name followed by parentheses:

    func sayMyName() {
        print("Hey, Andy!")
    }
    
    sayMyName() // Hey, Andy!
    sayMyName() // Hey, Andy! (each call runs the body again)
    

    The two calls are only there to show the function working. For this lesson, submit the definition alone.

    Common mistakes

    • Indenting instead of adding braces. The code looks right and still has a header with no body.
    • Forgetting the closing brace. The compiler reaches the end of the code while still inside the body and reports a missing }.
    • Empty braces. func sayMyName() {} followed by the print line compiles, but the function has an empty body and the print line stands outside it. The message is printed by that loose line, and calling the function would print nothing.
    • Changing the name. Names are case-sensitive: saymyname is a different function from sayMyName.
    • Adding a call. The task didn't ask for one.

    Checking your fix

    Run the code in the editor above. A correct fix compiles without errors. To see the message itself, add a call such as sayMyName() in a program of your own: it prints Hey, Andy! once per call.

    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free