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:
- The function has no body. A function declaration in Swift needs
{ ... }after its header. Without the braces, the header is incomplete. - 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
printunder the header would look right to a person and still fail. Even once the header is fixed, aprintline 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
- Keep the header.
func sayMyName()is correct: thefunckeyword, the exact name the task asks for and empty parentheses. - Open the body. Add
{after the header, on the same line. - Move the statement inside. The
printline goes between the braces, indented by four spaces so readers can see it belongs to the function. - Close the body. Put
}on its own line below the statement. - 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
-> Typeafter the parentheses, the function returns nothing. Its only effect is the printed line. printends 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 theprintline compiles, but the function has an empty body and theprintline 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:
saymynameis a different function fromsayMyName. - 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.
