What the code is supposed to do
The program defines a function welcomeUser and calls it from mainFunction. It should print two lines, in this order:
Hey coder!
Welcome back!
Here is the code you start with:
func welcomeUser() {
fmt.Println("Welcome back!")
fmt.Println("Hey coder!")
}
func mainFunction() {
welcomeUser()
}
It compiles and runs, but the two messages appear the wrong way round: Welcome back! first, then Hey coder!.
What goes wrong
There is no compile error here, and the function is called correctly. mainFunction calls welcomeUser() exactly once, with the name followed by parentheses. The mistake is in the body.
Go runs the statements of a function body from top to bottom, in the order they are written. The first fmt.Println prints Welcome back! and the second prints Hey coder!. The output therefore follows the order of the lines in the source, not the order the messages appear in your head.
Order of statements and order of declarations
These are two different ideas, and the lesson's title hints at the second one.
- Statements inside a function run in the order they are written. Swapping two lines changes the output.
- Declarations at package level can come in any order. A function may be called from code placed above its definition, because Go sees all package-level declarations together. Here
mainFunctioncould come first andwelcomeUserafter it, and the program would behave the same.
So the fix is not to move the functions around. It is to reorder the two print lines inside the one function.
Fixing it step by step
- Find where the order is decided.
welcomeUser()is called once, so the order of the output comes from the body ofwelcomeUser. - Compare the lines with the expected output. The first line printed should be
Hey coder!, but the firstPrintlnin the body printsWelcome back!. - Swap the two
Printlnlines. Keep the strings exactly as they are; only their positions change. - Leave
mainFunctionalone. It already calls the function once.
The fixed code in Go
func welcomeUser() {
fmt.Println("Hey coder!")
fmt.Println("Welcome back!")
}
func 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 "fmt.Println()" statements' order inside our function.
*/
The comment block at the end is only an explanation, and Go ignores everything between /* and */. A few details are worth knowing:
- Each
Printlnends its own line. That is why the two messages land on separate lines without any\nin the strings. - The text must match exactly. Capitals, the space and the exclamation mark are all part of the expected output.
- Comments in Go start with
//for one line or sit between/*and*/for several lines.
Running the fixed function prints:
Hey coder!
Welcome back!
Common mistakes
- Editing
mainFunction. Adding a second call would print the pair of messages twice, and neither change touches the order inside the pair. - Changing the strings instead of the order. Typing
Hey coder!into the first line andWelcome back!into the second works, but the swap is simpler and keeps the text untouched. - Moving the function definition. It makes no difference, since Go does not care which function is declared first.
- Dropping the parentheses in the call.
welcomeUserwithout()is rejected by the compiler as a value that is not used.
Checking your fix
Run the code in the editor above. The output should be exactly the two lines shown earlier, Hey coder! followed by Welcome back!, each on its own line.