Lesson 20 of 55
9 mins readPython Multiple Return Values & Unpacking
In Plain English
Lua has first-class support for returning multiple values from a function: `return x, y, z`. Callers unpack them in multiple assignments: `local x, y, z = get_coords()`.
Deep Dive: How It Works
Multiple Return Syntax: `return val1, val2, val3`.
Unpacking Idiom: `local min, max = find_min_max(list)`.
Adjusting to Context: When used in expression lists, multiple returns expand fully; when wrapped in parentheses `(func())`, only the first return value is retained.
Core Rules to Remember

Native Multiple Returns: Eliminates the need for tuple or wrapper objects.

Parentheses Truncation: Wrapping `(func())` truncates return list to the single primary value.
Live Interactive Example
Hit Run Code to see it liveReturning Min and Max Elements
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Output Console
Click "Run Code" to view the rendered output.
How it works: min_max returned two values unpacked into low and high.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Return Status Code and Message
Define `local function get_status() return 200, "OK" end`.
Unpack `local code, msg = get_status()` and print `"Status: " .. code .. " " .. msg`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.