Challenge - 5 Problems
Go Function Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with multiple parameters
What is the output of this Go program?
Go
package main import "fmt" func addMultiply(a int, b int) (int, int) { return a + b, a * b } func main() { sum, product := addMultiply(3, 4) fmt.Println(sum, product) }
Attempts:
2 left
💡 Hint
Look at the order of return values and how they are assigned.
✗ Incorrect
The function returns sum first (3+4=7) and then product (3*4=12). The variables sum and product receive these values in order.
❓ Predict Output
intermediate2:00remaining
Output of function with variadic parameter
What does this Go program print?
Go
package main import "fmt" func sumAll(nums ...int) int { total := 0 for _, n := range nums { total += n } return total } func main() { fmt.Println(sumAll(1, 2, 3, 4)) }
Attempts:
2 left
💡 Hint
Variadic parameters allow passing any number of arguments.
✗ Incorrect
The function sums all numbers: 1+2+3+4 = 10.
❓ Predict Output
advanced2:00remaining
Output of function with pointer parameter
What is printed by this Go program?
Go
package main import "fmt" func increment(x *int) { *x = *x + 1 } func main() { a := 5 increment(&a) fmt.Println(a) }
Attempts:
2 left
💡 Hint
The function changes the value at the pointer's address.
✗ Incorrect
The function increments the value pointed by x, so a changes from 5 to 6.
❓ Predict Output
advanced2:00remaining
Output of function with named return parameters
What does this Go program print?
Go
package main import "fmt" func divide(dividend, divisor int) (result int, err string) { if divisor == 0 { err = "division by zero" return } result = dividend / divisor return } func main() { res, err := divide(10, 0) fmt.Println(res, err) }
Attempts:
2 left
💡 Hint
Named return parameters are zero-valued if not set explicitly.
✗ Incorrect
When divisor is zero, err is set but result remains zero (default int). So output is '0 division by zero'.
🧠 Conceptual
expert2:00remaining
Function parameter passing behavior
Which statement correctly describes how Go passes function parameters?
Attempts:
2 left
💡 Hint
Think about how slices and maps behave differently from basic types.
✗ Incorrect
Go passes all parameters by value (copy). However, slices and maps contain pointers internally, so changes to their elements affect the original data.