A function declaration in Go defines a reusable block of code with a name, optional parameters, and an optional return type. It tells the program what the function does when called.
Click to reveal answer
beginner
How do you declare a simple function named greet that prints "Hello" in Go?
You write:
func greet() {
fmt.Println("Hello")
}
This declares a function named greet with no parameters and no return value.
Click to reveal answer
intermediate
What is the syntax to declare a function with parameters in Go?
You list parameters inside parentheses after the function name, each with a name and type. For example:
func add(a int, b int) int {
return a + b
}
This function takes two integers and returns an integer.
Click to reveal answer
intermediate
Can a Go function return multiple values? How?
Yes, Go functions can return multiple values by listing return types in parentheses. Example:
func swap(x, y string) (string, string) {
return y, x
}
This function returns two strings.
Click to reveal answer
beginner
What keyword starts a function declaration in Go?
The keyword func starts every function declaration in Go. It signals that a function is being defined.
Click to reveal answer
Which keyword is used to declare a function in Go?
Afunc
Bfunction
Cdef
Dfun
✗ Incorrect
In Go, the keyword func is used to declare functions.
How do you declare a function named sum that takes two integers and returns an integer?
Afunction sum(a, b int) int {}
Bfunc sum(a int, b int) {}
Cfunc sum(a int, b int) int {}
Ddef sum(a int, b int) int {}
✗ Incorrect
Option C correctly uses Go syntax with func, parameter types, and return type.
Can a Go function return more than one value?
ANo, Go does not support return values
BNo, only one return value is allowed
CYes, but only if parameters are also multiple
DYes, by listing multiple return types in parentheses
✗ Incorrect
Go supports multiple return values by listing them in parentheses after the function parameters.
What is the correct way to declare a function with no parameters and no return value?
Afunc hello() {}
Bfunc hello {}
Cfunc hello() -> void {}
Dfunction hello() {}
✗ Incorrect
In Go, empty parentheses indicate no parameters, and no return type means no return value.
Which of these is a valid function declaration in Go?
Option B correctly declares parameter type and return type with proper syntax.
Explain how to declare a function in Go that takes parameters and returns a value.
Think about the order: func, name, parameters, return type, then body.
You got /5 concepts.
Describe how Go functions can return multiple values and why this might be useful.
Remember Go often returns error as a second value.
You got /3 concepts.
Practice
(1/5)
1. What keyword is used to declare a function in Go?
easy
A. func
B. function
C. def
D. fun
Solution
Step 1: Recall Go function syntax
In Go, functions are declared using the keyword func.
Step 2: Compare options
Only func uses func, which is correct for Go.
Final Answer:
func -> Option A
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
Step 1: Understand Go function parameter syntax
In Go, parameters are declared as name type. So a int, b int is correct.
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.
Final Answer:
func add(a int, b int) int {} -> Option D
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
Step 1: Understand function call and return
The function multiply takes two integers and returns their product.
Step 2: Calculate multiply(3, 4)
3 * 4 = 12, so the function returns 12, which is printed.
Final Answer:
12 -> Option A
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
Step 1: Check function syntax
The function starts with func, has parameter and return type correctly.
Step 2: Look for missing parts
The function body is missing the closing brace } to end the function.
Final Answer:
Missing closing brace '}' -> Option C
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
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.
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.
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
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 unknownstrongwhile earning +1 Grip toward your certificate.