Bird
Raised Fist0
Goprogramming~10 mins

Multiple return values in Go - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Multiple return values
Function called
↓
Execute function body
↓
Prepare multiple return values
↓
Return values to caller
↓
Caller receives multiple values
↓
Use returned values
The function runs, creates multiple values, returns them together, and the caller receives and uses them.
Execution Sample
Go
func divide(a, b int) (int, int) {
    quotient := a / b
    remainder := a % b
    return quotient, remainder
}

q, r := divide(10, 3)
This code divides 10 by 3, returning both quotient and remainder.
Execution Table
StepActionVariablesReturn ValuesCaller Variables
1Call divide(10, 3)a=10, b=3--
2Calculate quotient = 10 / 3quotient=3--
3Calculate remainder = 10 % 3remainder=1--
4Return quotient and remainder-3, 1-
5Assign returned values to q, r--q=3, r=1
💡 Function returns two values and caller stores them in q and r.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5
a10101010
b3333
quotient-33-
remainder--1-
q---3
r---1
Key Moments - 2 Insights
Why can a function return two values at once?
In Go, functions can return multiple values as a tuple. The execution_table row 4 shows the function returning both quotient and remainder together.
How does the caller receive multiple return values?
The caller assigns the returned values to multiple variables separated by commas, as shown in execution_table row 5 where q and r get the values.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'quotient' after step 2?
A10
B3
C1
D0
💡 Hint
Check the 'Variables' column at step 2 in the execution_table.
At which step does the function return the two values?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the 'Return Values' column in the execution_table.
If we changed the call to q := divide(10, 3), what would happen?
Aq gets a tuple of quotient and remainder
Bq gets the quotient, remainder is ignored
CCompilation error because multiple values returned but only one variable to receive
DFunction returns only one value automatically
💡 Hint
Go requires the number of variables on the left to match the number of returned values, see variable assignment in execution_table row 5.
Concept Snapshot
func name(params) (type1, type2) {
  // code
  return val1, val2
}

caller1, caller2 := name(args)

- Functions can return multiple values.
- Caller must receive all returned values.
- Useful for returning results and errors together.
Full Transcript
This example shows how a Go function can return multiple values. The function divide takes two integers and returns two integers: the quotient and remainder of division. When called with 10 and 3, it calculates quotient as 3 and remainder as 1. Then it returns both values together. The caller receives these two values and stores them in variables q and r. This way, Go functions can return multiple results at once, which is helpful for many tasks like returning a result and an error. The execution table traces each step: calling the function, calculating values, returning them, and assigning them to caller variables.

Practice

(1/5)
1. In Go, what does it mean when a function returns multiple values?
easy
A. The function sends back more than one result at the same time.
B. The function can only return one value but multiple times.
C. The function returns a list of values inside a slice.
D. The function returns values only if an error occurs.

Solution

  1. Step 1: Understand function return types in Go

    Go functions can list multiple types in parentheses to return several values together.
  2. Step 2: Interpret multiple return values meaning

    This means the function sends back more than one result at once, not just one or a list.
  3. Final Answer:

    The function sends back more than one result at the same time. -> Option A
  4. Quick Check:

    Multiple return values = more than one result [OK]
Hint: Multiple return means several results sent together [OK]
Common Mistakes:
  • Thinking it returns a slice or array
  • Assuming only one value can be returned
  • Confusing multiple returns with repeated calls
2. Which of the following is the correct syntax to declare a function in Go that returns two integers?
easy
A. func add() int, int { return 1, 2 }
B. func add() [int, int] { return 1, 2 }
C. func add() { return 1, 2 }
D. func add() (int, int) { return 1, 2 }

Solution

  1. Step 1: Check Go function return type syntax

    In Go, multiple return types are enclosed in parentheses like (int, int).
  2. Step 2: Match syntax with options

    func add() (int, int) { return 1, 2 } correctly uses (int, int) and returns two integers separated by a comma.
  3. Final Answer:

    func add() (int, int) { return 1, 2 } -> Option D
  4. Quick Check:

    Multiple returns use parentheses (type1, type2) [OK]
Hint: Use parentheses around multiple return types [OK]
Common Mistakes:
  • Omitting parentheses around return types
  • Using square brackets instead of parentheses
  • Returning multiple values without declaring types
3. What will be the output of this Go program?
package main
import "fmt"
func divide(a, b int) (int, int) {
    return a / b, a % b
}
func main() {
    q, r := divide(10, 3)
    fmt.Println(q, r)
}
medium
A. 3 3
B. 3 0
C. 3 1
D. 10 3

Solution

  1. Step 1: Understand the divide function

    The function returns two values: quotient (a / b) and remainder (a % b).
  2. Step 2: Calculate divide(10, 3)

    10 divided by 3 is 3 with remainder 1, so returns (3, 1).
  3. Final Answer:

    3 1 -> Option C
  4. Quick Check:

    10/3=3 remainder 1 [OK]
Hint: Remember / is quotient, % is remainder [OK]
Common Mistakes:
  • Confusing remainder with quotient
  • Swapping the order of returned values
  • Expecting a single return value
4. Identify the error in this Go function that returns two strings:
func names() (string, string) {
    return "Alice"
}
medium
A. Missing second return value causes a compile error.
B. Function should return only one string, not two.
C. Return statement syntax is incorrect.
D. Function must have parameters to return multiple values.

Solution

  1. Step 1: Check function return signature

    The function declares it returns two strings (string, string).
  2. Step 2: Check return statement

    It returns only one string "Alice", missing the second value, causing a compile error.
  3. Final Answer:

    Missing second return value causes a compile error. -> Option A
  4. Quick Check:

    Return count must match declared types [OK]
Hint: Return values count must match function signature [OK]
Common Mistakes:
  • Returning fewer values than declared
  • Assuming parameters affect return count
  • Incorrect return statement syntax
5. You want to write a Go function that returns the minimum and maximum values from a slice of integers. Which function correctly returns both values?
hard
A. func minMax(nums []int) int { min, max := nums[0], nums[0] for _, v := range nums { if v < min { min = v } if v > max { max = v } } return min, max }
B. func minMax(nums []int) (int, int) { min, max := nums[0], nums[0] for _, v := range nums { if v < min { min = v } if v > max { max = v } } return min, max }
C. func minMax(nums []int) (int) { min, max := nums[0], nums[0] for _, v := range nums { if v < min { min = v } if v > max { max = v } } return min, max }
D. func minMax(nums []int) (int, int) { min := nums[0] max := nums[0] for i := 1; i < len(nums); i++ { if nums[i] < min { min = nums[i] } if nums[i] > max { max = nums[i] } } return max, min }

Solution

  1. Step 1: Check function return types

    Function must return two integers (min and max), so signature must be (int, int).
  2. Step 2: Verify logic and return order

    func minMax(nums []int) (int, int) { min, max := nums[0], nums[0] for _, v := range nums { if v < min { min = v } if v > max { max = v } } return min, max } correctly initializes min and max, loops through nums, updates min and max, and returns min, max in correct order.
  3. Final Answer:

    func minMax(nums []int) (int, int) { min, max := nums[0], nums[0] for _, v := range nums { if v < min { min = v } if v > max { max = v } } return min, max } -> Option B
  4. Quick Check:

    Return (min, max) with correct signature and logic [OK]
Hint: Return multiple values with matching signature and correct order [OK]
Common Mistakes:
  • Wrong return type signature
  • Returning values in wrong order
  • Returning only one value instead of two
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard