0
0
Goprogramming~10 mins

Function declaration in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a function named greet.

Go
func [1]() {
    fmt.Println("Hello!")
}
Drag options to blanks, or click blank then click option'
Ahello
Bprint
CsayHello
Dgreet
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.
2fill in blank
medium

Complete 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'
Aint
Bstring
Cbool
Dfloat64
Attempts:
3 left
💡 Hint
Common Mistakes
Using a return type that does not match the returned value.
Forgetting to specify the return type.
3fill in blank
hard

Fix 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'
Avar name
Bstring
Cname
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Writing the type before the parameter name.
Using keywords like var inside parameter list.
4fill in blank
hard

Fill 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'
Aint
Bstring
Cfloat64
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using different types for parameters that are added.
Using non-integer types when integers are expected.
5fill in blank
hard

Fill 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'
Astring
Bint
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up parameter types.
Using wrong return type.