Named return values are variables defined in a function's signature that hold the return values. They allow you to return values without explicitly specifying them in the return statement.
Click to reveal answer
beginner
How do you declare named return values in a Go function?
You declare named return values by specifying variable names and types inside the parentheses after the function parameters, like: func example() (result int, err error).
Click to reveal answer
intermediate
What happens if you use a bare return statement in a function with named return values?
A bare return returns the current values of the named return variables. This means you don't have to specify the variables explicitly in the return statement.
Click to reveal answer
intermediate
Can named return values improve code readability? Why or why not?
Yes, named return values can improve readability by giving meaningful names to return values, making it clear what each returned value represents without needing extra comments.
Click to reveal answer
beginner
Show a simple Go function using named return values to return the sum and difference of two integers.
Example:
func calc(a, b int) (sum int, diff int) {
sum = a + b
diff = a - b
return
}
This function returns sum and diff without explicitly listing them in the return statement.
Click to reveal answer
In Go, how do you define named return values in a function?
ABy naming variables in the return type parentheses
BBy declaring variables inside the function body
CBy using global variables
DBy using pointers only
✗ Incorrect
Named return values are declared by naming variables in the return type parentheses of the function signature.
What does a bare return statement do in a function with named return values?
AReturns the current values of named return variables
BReturns zero values
CCauses a compile error
DReturns nothing
✗ Incorrect
A bare return returns the current values of the named return variables without needing to specify them.
Which of the following is a benefit of using named return values?
AThey allow multiple return types
BThey improve code readability by naming return values
CThey prevent runtime errors
DThey make functions run faster
✗ Incorrect
Named return values improve readability by giving meaningful names to the returned data.
What is the output of this Go function?
func example() (x int) {
x = 5
return
}
A call to example() returns:
A0
BAn error
C5
DUndefined
✗ Incorrect
The function sets x to 5 and returns it using a bare return, so the output is 5.
Can you mix named and unnamed return values in a Go function signature?
AOnly if the function has one return value
BNo, all must be named or unnamed
COnly in Go versions before 1.10
DYes, but it's discouraged
✗ Incorrect
You can mix named and unnamed return values, but it is generally discouraged for clarity.
Explain what named return values are in Go and how they affect the return statement.
Think about how naming return variables changes how you write return statements.
You got /3 concepts.
Write a simple Go function using named return values to return the quotient and remainder of two integers.
Use division and modulus operators and name the return values clearly.
You got /3 concepts.
Practice
(1/5)
1. What is the main benefit of using named return values in Go functions?
easy
A. They prevent the function from returning any value.
B. They allow returning multiple values without specifying types.
C. They automatically convert return types to strings.
D. They make the function's return values easier to understand and use.
Solution
Step 1: Understand named return values
Named return values give explicit names to the outputs of a function, making it clear what each returned value represents.
Step 2: Identify the benefit
Because the return values are named, the code becomes easier to read and understand, especially when returning multiple values.
Final Answer:
They make the function's return values easier to understand and use. -> Option D
Quick Check:
Named return values improve readability = D [OK]
Hint: Named returns clarify output meaning quickly [OK]
Common Mistakes:
Thinking named returns change return types
Believing named returns prevent returning values
Confusing named returns with automatic type conversion
2. Which of the following Go function signatures correctly uses named return values?
easy
A. func add(a, b int) (int sum) { return a + b }
B. func add(a, b int) int sum { return a + b }
C. func add(a, b int) (sum int) { return a + b }
D. func add(a, b int) sum int { return a + b }
Solution
Step 1: Check syntax for named return values
Named return values are declared inside parentheses with the type after the name, like (sum int).
Step 2: Identify correct syntax
func add(a, b int) (sum int) { return a + b } uses (sum int) correctly. Options B, C, and D have incorrect syntax for named returns.
Final Answer:
func add(a, b int) (sum int) { return a + b } -> Option C
Quick Check:
Named returns syntax = (name type) [OK]
Hint: Named returns use (name type) inside parentheses [OK]
Common Mistakes:
Placing type before name in return signature
Omitting parentheses around return values
Using invalid order like 'int sum'
3. What will be the output of this Go program?
package main
import "fmt"
func divide(a, b int) (result int, err string) {
if b == 0 {
err = "division by zero"
return
}
result = a / b
return
}
func main() {
r, e := divide(10, 0)
fmt.Println(r, e)
}
medium
A. 10 division by zero
B. 0 division by zero
C. 0
D. Runtime error
Solution
Step 1: Analyze divide function behavior
If b is zero, err is set to "division by zero" and function returns immediately. result is not set, so defaults to zero.
Step 2: Check main function output
Calling divide(10, 0) returns result=0 and err="division by zero". fmt.Println prints both separated by space.
Final Answer:
0 division by zero -> Option B
Quick Check:
Named returns default zero + error string = A [OK]
Hint: Named returns default zero values if not set [OK]
Common Mistakes:
Expecting result to be 10 when dividing by zero
Assuming runtime error instead of handled error string
Forgetting named returns default zero values
4. Which of the following best improves this Go function using named return values:
func multiply(a, b int) (product int) {
product = a * b
return product
}
medium
A. The return statement should be just 'return' without specifying 'product'.
B. The function must return two values, not one.
C. Named return values cannot be assigned inside the function.
D. The function should not have a return statement.
Solution
Step 1: Understand named return values usage
When using named return values, you can simply use 'return' without arguments to return the named variables.
Step 2: Identify the improvement in return statement
Here, 'return product' is allowed but redundant. The idiomatic way is just 'return' to return named values.
Final Answer:
The return statement should be just 'return' without specifying 'product'. -> Option A
Quick Check:
Named returns allow bare 'return' [OK]
Hint: Use bare 'return' with named return values [OK]
Common Mistakes:
Returning named variable explicitly instead of bare return
Thinking named returns require multiple return values
Believing assignment to named returns is disallowed
5. Given this Go function with named return values, what will be the output?
package main
import "fmt"
func stats(nums []int) (sum, count int) {
for _, n := range nums {
if n > 0 {
sum += n
count++
}
}
return
}
func main() {
s, c := stats([]int{3, -1, 0, 4, 5})
fmt.Printf("Sum: %d, Count: %d\n", s, c)
}
hard
A. Sum: 12, Count: 3
B. Sum: 12, Count: 5
C. Sum: 0, Count: 0
D. Sum: 3, Count: 1
Solution
Step 1: Analyze the loop filtering positive numbers
The function loops over nums and adds only positive numbers to sum and increments count.
Step 2: Calculate sum and count for given slice
Positive numbers are 3, 4, 5. Sum = 3+4+5=12, Count = 3.