In this lesson
- It starts with a question. A function add_tip(bill) has two lines inside: total = bill + 5 and return total. The main program then has two lines: add_tip(20) and print(total). No other line mentions total. What happens at the print?
- The idea you will find. Names made inside a function stay inside it. — The parameter kg and the name amount existed only while cost was running. When it returned, they were thrown away and only the returned value came out. That is why the main program had to catch it in bill. It also means two functions can both use a name like total without getting in each other’s way.
- How you learn it. By doing: sorting, a slider, matching.
- Length. 6 short steps. Progress saves on this phone.
Part of Python: Functions That Return
Writing functions that give you something back — and knowing what happens if they do not.