What if your functions could hand you answers like a helpful friend every time?
Why Return values in Go? - Purpose & Use Cases
Imagine you are baking cookies and want to share how many you made with your friend. Without a clear way to tell them the exact number, you have to keep repeating or writing it down manually every time.
Manually tracking and sharing results is slow and confusing. You might forget the number or mix it up, causing mistakes and extra work.
Return values let a function send back results automatically. This way, you get the exact answer every time without extra steps or confusion.
func bakeCookies() {
fmt.Println("Baked 12 cookies")
}
// No way to get the number backfunc bakeCookies() int {
return 12
}
// Now you can use the number anywhereReturn values make your code clear and reusable by passing results directly where needed.
When calculating a bill, a function can return the total amount so you can print it, save it, or use it for discounts without repeating calculations.
Return values send results from functions back to the caller.
They avoid manual tracking and reduce errors.
They make code easier to use and understand.
Practice
Solution
Step 1: Understand the purpose of return values
Return values are used to send results from a function back to the caller.Step 2: Identify the correct description
Only It sends a result back to the place where the function was called. correctly describes this purpose.Final Answer:
It sends a result back to the place where the function was called. -> Option DQuick Check:
Return value = sends result back [OK]
- Confusing return values with variable declarations
- Thinking return values name the function
- Assuming return values are comments
Solution
Step 1: Check function syntax for return type
In Go, the return type is specified after parentheses: func name() type { ... }Step 2: Verify each option
func add() int { return 5 } correctly declares a function returning int. Others have syntax errors.Final Answer:
func add() int { return 5 } -> Option AQuick Check:
Return type after () = int [OK]
- Placing return type before function name
- Omitting return type when returning a value
- Incorrect function syntax
func multiply(a, b int) int {
return a * b
}
func main() {
result := multiply(3, 4)
fmt.Println(result)
}Solution
Step 1: Check for required imports
The code uses fmt.Println(result) but lacks import "fmt", causing a compile error: undefined fmt.Step 2: Verify function logic
The multiply function correctly computes 3*4=12, but the program won't compile or run without the import.Final Answer:
Error: missing import -> Option CQuick Check:
fmt.Println needs import "fmt" [OK]
- Forgetting import "fmt"
- Computing 3*4=12 but ignoring missing import
- Assuming code runs without imports
func divide(a, b int) (int, int) {
return a / b
}Solution
Step 1: Check return type declaration
The function declares two return values (int, int) but returns only one value.Step 2: Identify mismatch in return statement
Returning a single value causes a compile error because two values are expected.Final Answer:
Function returns only one value but declares two return types. -> Option BQuick Check:
Return count must match declared types [OK]
- Returning fewer values than declared
- Wrong operator usage confusion
- Ignoring function signature
Solution
Step 1: Define function with two return types
The function must declare two return types (int, int) for sum and product.Step 2: Return sum and product as two values
Return both values separated by a comma: return a+b, a*b.Final Answer:
func calc(a, b int) (int, int) { return a+b, a*b } -> Option AQuick Check:
Two return values with matching types [OK]
- Returning multiple values with single return type
- Returning combined expression instead of two values
- Incorrect return type syntax
Ready to prove it?
Pass the test and this pattern moves unknown
