Challenge - 5 Problems
Function Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Go program using a function?
Look at this Go program. What will it print when run?
Go
package main import "fmt" func greet(name string) string { return "Hello, " + name + "!" } func main() { message := greet("Alice") fmt.Println(message) }
Attempts:
2 left
💡 Hint
The function greet returns a greeting message with the name included.
✗ Incorrect
The function greet takes a name and returns a greeting string. The main function calls greet with "Alice" and prints the returned message.
🧠 Conceptual
intermediate1:30remaining
Why use functions in programming?
Which of these is the main reason to use functions in programs?
Attempts:
2 left
💡 Hint
Think about how functions help with repeating tasks.
✗ Incorrect
Functions let you write code once and use it many times, making programs shorter and easier to manage.
❓ Predict Output
advanced2:00remaining
What is the output of this Go program with multiple function calls?
What will this Go program print when run?
Go
package main import "fmt" func add(a, b int) int { return a + b } func main() { x := add(2, 3) y := add(x, 4) fmt.Println(y) }
Attempts:
2 left
💡 Hint
Add the numbers step by step.
✗ Incorrect
First add(2,3) returns 5, then add(5,4) returns 9, which is printed.
🔧 Debug
advanced2:00remaining
What error does this Go program cause?
This Go program tries to use a function but has a problem. What error will it cause?
Go
package main import "fmt" func multiply(a int, b int) int { return a * b } func main() { result := multiply(3) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Check how many arguments the multiply function needs.
✗ Incorrect
The multiply function requires two arguments, but only one is given, causing a compile error.
🚀 Application
expert2:30remaining
How many times is the function called in this Go program?
Count how many times the function 'countCalls' is called when this program runs.
Go
package main import "fmt" var calls int func countCalls() { calls++ } func main() { for i := 0; i < 3; i++ { countCalls() } if calls > 0 { countCalls() } fmt.Println(calls) }
Attempts:
2 left
💡 Hint
Count calls inside the loop and the one after the loop.
✗ Incorrect
The function is called 3 times in the loop and once more after, totaling 4 calls.