Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a function named greet.
Go
func [1]() { fmt.Println("Hello!") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a name that is not the function's intended name.
Forgetting to write the function name after
func.✗ Incorrect
The function name is greet. This declares a function called greet that prints "Hello!".
2fill in blank
mediumComplete the code to declare a function that returns an integer 5.
Go
func getNumber() [1] { return 5 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a return type that does not match the returned value.
Forgetting to specify the return type.
✗ Incorrect
The function returns an integer, so the return type is int.
3fill in blank
hardFix the error in the function declaration to accept a string parameter named name.
Go
func greet([1] string) { fmt.Println("Hello,", name) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Writing the type before the parameter name.
Using keywords like
var inside parameter list.✗ Incorrect
The parameter name should be name followed by its type string.
4fill in blank
hardFill both blanks to declare a function that takes two integers and returns their sum.
Go
func add(a [1], b [2]) int { return a + b }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different types for parameters that are added.
Using non-integer types when integers are expected.
✗ Incorrect
Both parameters are integers, so both blanks should be int.
5fill in blank
hardFill all three blanks to declare a function that takes a string and an int, and returns a formatted string.
Go
func formatMessage(name [1], age [2]) [3] { return fmt.Sprintf("%s is %d years old", name, age) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up parameter types.
Using wrong return type.
✗ Incorrect
The function takes a string and an int, and returns a string.