Bird
Raised Fist0
Goprogramming~10 mins

Function declaration in Go - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What keyword is used to declare a function in Go?
easy
A. func
B. function
C. def
D. fun

Solution

  1. Step 1: Recall Go function syntax

    In Go, functions are declared using the keyword func.
  2. Step 2: Compare options

    Only func uses func, which is correct for Go.
  3. Final Answer:

    func -> Option A
  4. Quick Check:

    Function declaration keyword = func [OK]
Hint: Remember: Go uses 'func' to start a function [OK]
Common Mistakes:
  • Using 'function' like JavaScript
  • Using 'def' like Python
  • Using 'fun' which is invalid
2. Which of the following is the correct syntax to declare a function named add that takes two integers and returns an integer?
easy
A. function add(a int, b int) int {}
B. func add(int a, int b) int {}
C. func add(a, b int) {}
D. func add(a int, b int) int {}

Solution

  1. Step 1: Understand Go function parameter syntax

    In Go, parameters are declared as name type. So a int, b int is correct.
  2. Step 2: Check return type and function keyword

    Function must start with func and specify return type int. func add(a int, b int) int {} matches this exactly.
  3. Final Answer:

    func add(a int, b int) int {} -> Option D
  4. Quick Check:

    Parameter syntax = name type [OK]
Hint: Parameters: name then type, return type after parentheses [OK]
Common Mistakes:
  • Putting type before name in parameters
  • Using 'function' keyword instead of 'func'
  • Omitting return type when needed
3. What will be the output of this Go program?
package main
import "fmt"
func multiply(x int, y int) int {
    return x * y
}
func main() {
    fmt.Println(multiply(3, 4))
}
medium
A. 12
B. 7
C. 34
D. Error

Solution

  1. Step 1: Understand function call and return

    The function multiply takes two integers and returns their product.
  2. Step 2: Calculate multiply(3, 4)

    3 * 4 = 12, so the function returns 12, which is printed.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    3 times 4 = 12 [OK]
Hint: Multiply parameters and print result [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Concatenating numbers as strings
  • Expecting syntax error due to function
4. Identify the error in this Go function declaration:
func greet(name string) string {
    return "Hello, " + name
medium
A. Incorrect function keyword
B. Wrong parameter type
C. Missing closing brace '}'
D. Missing return type

Solution

  1. Step 1: Check function syntax

    The function starts with func, has parameter and return type correctly.
  2. Step 2: Look for missing parts

    The function body is missing the closing brace } to end the function.
  3. Final Answer:

    Missing closing brace '}' -> Option C
  4. Quick Check:

    Every function must end with '}' [OK]
Hint: Count opening and closing braces carefully [OK]
Common Mistakes:
  • Forgetting to close function body with '}'
  • Confusing parameter types
  • Omitting return type
5. How would you declare a Go function named filterEven that takes a slice of integers and returns a new slice containing only even numbers?
hard
A. func filterEven(nums int) int { var evens int for _, n := range nums { if n%2 == 0 { evens += n } } return evens }
B. func filterEven(nums []int) []int { var evens []int for _, n := range nums { if n%2 == 0 { evens = append(evens, n) } } return evens }
C. func filterEven(nums []int) { for _, n := range nums { if n%2 == 0 { fmt.Println(n) } } }
D. func filterEven(nums []int) []int { for n := range nums { if n%2 == 0 { return nums } } return nil }

Solution

  1. Step 1: Check parameter and return types

    The function must take a slice of integers []int and return a slice of integers []int. func filterEven(nums []int) []int { var evens []int for _, n := range nums { if n%2 == 0 { evens = append(evens, n) } } return evens } matches this.
  2. Step 2: Verify logic to filter even numbers

    func filterEven(nums []int) []int { var evens []int for _, n := range nums { if n%2 == 0 { evens = append(evens, n) } } return evens } creates a new slice, loops over input, appends even numbers, and returns the new slice. This is correct.
  3. Final Answer:

    func filterEven(nums []int) []int { var evens []int for _, n := range nums { if n%2 == 0 { evens = append(evens, n) } } return evens } -> Option B
  4. Quick Check:

    Filter slice by appending evens [OK]
Hint: Use slice, loop, append evens, return new slice [OK]
Common Mistakes:
  • Wrong parameter or return types
  • Returning inside loop prematurely
  • Printing instead of returning filtered slice
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard