0
0
Goprogramming~10 mins

Function parameters 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 with one integer parameter named x.

Go
func printNumber([1] int) {
    fmt.Println(x)
}
Drag options to blanks, or click blank then click option'
Anumber
Bnum
Cvalue
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from the one used inside the function causes an error.
2fill in blank
medium

Complete the code to declare a function that takes two string parameters: first and last.

Go
func fullName([1] string, last string) string {
    return first + " " + last
}
Drag options to blanks, or click blank then click option'
Afirst
Bname
Cfname
Dlast
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the one used in the function body.
3fill in blank
hard

Fix the error in the function parameter declaration to accept a float64 parameter named price.

Go
func calculateTax([1] float64) float64 {
    return price * 0.1
}
Drag options to blanks, or click blank then click option'
Aprice
Btax
Camount
Dcost
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from the one used inside the function causes an undefined variable error.
4fill in blank
hard

Fill both blanks to declare a function that takes an int parameter age and a string parameter name.

Go
func greet([1] int, [2] string) string {
    return fmt.Sprintf("Hello %s, you are %d years old", name, age)
}
Drag options to blanks, or click blank then click option'
Aage
Byears
Cname
Dperson
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping parameter names or using names not matching the function body.
5fill in blank
hard

Fill all three blanks to declare a function that takes a string parameter title, an int parameter pages, and a float64 parameter price.

Go
func bookInfo([1] string, [2] int, [3] float64) string {
    return fmt.Sprintf("%s has %d pages and costs $%.2f", title, pages, price)
}
Drag options to blanks, or click blank then click option'
Atitle
Bpages
Cprice
Dcost
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names that do not match the variables used inside the function.