loop
Function return

Module 4 · Function return · Lesson 3 of 4

Lists and functions

A function's result doesn't have to be a single number. It can hand back a whole list — build it inside, then return the finished thing. This is how you write functions that produce collections.

Run it yourselfruns in your browser · Python
main.py

But lists behave differently from plain numbers when they go into a function. Remember from earlier that a list name points at a shared location in memory — and passing it to a function passes that same reference. So a function can reach in and change the caller's own list.

A function can change the list you pass it
def grow(lst): lst.append(4)
↓ the caller's list
nums =1234

That can be exactly what you want (a function that fills or updates a list) — or a nasty surprise, if you assumed your original was safe. The animation above is the same aliasing idea from the lists module, now crossing a function boundary.

Run it yourselfruns in your browser · Python
main.py

The rule of thumb: if a function should leave its input untouched, work on a copy (lst[:]) and return the new list. If it's meant to update the list in place, change it directly — and it's polite to make that obvious in the function's name.