0
0
Goprogramming~10 mins

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

Choose your learning style9 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.