loop
Function return

Module 4 · Function return · Lesson 4 of 4

Examples

Time to put it together. Each of these is a small function that returns a result you can use elsewhere — and several build on each other, which is decomposition in action. Read each one, predict the output, then run it.

1. A leap year. A clean use of early returns to express a slightly fiddly rule one step at a time.

Run it yourselfruns in your browser · Python
main.py

2. How many days in a month. This one calls is_leap — a function using another function, exactly as intended.

Run it yourselfruns in your browser · Python
main.py

3. Day of the year. Built on top of days_in_month: sum the whole months before this one, then add the day. Three small functions, stacked.

Run it yourselfruns in your browser · Python
main.py

4. Prime numbers. A True/False function, then a comprehension that uses it to collect every prime below 30.

Run it yourselfruns in your browser · Python
main.py

5. Converting fuel consumption. Two tiny functions that turn a number into another number — the everyday bread and butter of returning results. (Using US gallons.)

Run it yourselfruns in your browser · Python
main.py

Notice the pattern across all five: take some inputs, compute, and return the answer — never just print it. A function that returns its result can be reused, combined, and tested. That's the whole point. Next in this module: bundling several values together with tuples and dictionaries.