0
0
Goprogramming~10 mins

Function execution flow 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 call the function greet.

Go
package main
import "fmt"
func greet() {
    fmt.Println("Hello!")
}
func main() {
    [1]()
}
Drag options to blanks, or click blank then click option'
Ahello
Bgreet
Cprint
Dsay
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong function name like 'print' or 'hello'.
Forgetting the parentheses after the function name.
2fill in blank
medium

Complete the code to return the sum of two integers from the function add.

Go
package main
import "fmt"
func add(a int, b int) int {
    return [1]
}
func main() {
    fmt.Println(add(3, 4))
}
Drag options to blanks, or click blank then click option'
Aa + b
Ba - b
Ca * b
Da / b
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to return the result.
3fill in blank
hard

Fix the error in the function call to multiply.

Go
package main
import "fmt"
func multiply(x int, y int) int {
    return x * y
}
func main() {
    result := multiply[1]
    fmt.Println(result)
}
Drag options to blanks, or click blank then click option'
A(5, 6
B5, 6
C5, 6)
D(5, 6)
Attempts:
3 left
💡 Hint
Common Mistakes
Missing closing parenthesis.
Including only one parenthesis or none.
4fill in blank
hard

Fill both blanks to create a function isEven that returns true if a number is even.

Go
package main
import "fmt"
func isEven(num int) bool {
    return num [1] 2 [2] 0
}
func main() {
    fmt.Println(isEven(4))
}
Drag options to blanks, or click blank then click option'
A%
B==
C!=
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulus (%).
Using != instead of == for comparison.
5fill in blank
hard

Fill all three blanks to create a function filterPositive that returns a slice of positive numbers from input.

Go
package main
import "fmt"
func filterPositive(nums []int) []int {
    var result []int
    for _, [1] := range nums {
        if [2] [3] 0 {
            result = append(result, num)
        }
    }
    return result
}
func main() {
    fmt.Println(filterPositive([]int{-2, 3, 0, 5, -1}))
}
Drag options to blanks, or click blank then click option'
Anum
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name in the loop or condition.
Using < instead of > in the condition.