Writing your first functions
A useful script often repeats the same job. A function lets you give that job a name so it can be performed whenever you call it. First we make functions without inputs; the next lesson teaches how to pass information into them.
A function is a named block of instructions
Use def followed by a name, parentheses and a colon. The indented lines are the function body. Python records the definition when it reaches it, but does not execute its body until you call the function with parentheses.
A name such as show_menu describes what the function does. Write one small purpose per function: that makes it easier to test and reuse without hunting through a large script.
Defining and calling are different actions
Writing def announce(): does not print anything by itself. Writing announce() calls the function, running the instructions inside it. Forgetting the parentheses when calling is a common cause of “nothing happened”.
Python processes statements in order. In a straightforward script, define the function before the line that calls it. Repeated calls reuse the same definition rather than duplicating the body.
Functions can contain logic you already know
A function body can contain if statements, loops and list operations from previous lessons. They use the same Python syntax, simply indented underneath the function definition.
Aim for names that explain intent, not implementation. A function called print_list suggests output; a function called sort_items suggests changing or returning a collection. We will make that distinction sharper when we introduce returned values.
Make the function small enough to understand
If a function has several unrelated responsibilities, divide it into smaller functions that call one another. That gives a future reader meaningful names instead of one long indentation ladder.
Functions are reusable code, not magic performance shortcuts. They can still contain bugs, and you still need to run them with different situations to find mistakes.
Try it yourself.
Choose a starting example or write something completely different. You control the code; the Run button executes what is in the editor, not a hidden preset.
A function stays idle until you call it.
Press Run Python to execute the editable code.
Remove the final call. Why is the output now empty?
Need a hint?
Python reads a function definition without executing its body.
On first Run, a pinned Python runtime downloads from jsDelivr. Code executes locally in your browser's Web Worker, not on our server. It cannot inspect your normal PC files or Windows hardware. Avoid entering secrets. Heavy/infinite loops are stopped by a time limit.
Check your understanding.
Try answering each question before opening its reference answer. Explain the idea aloud or change an example to test it for yourself.
What happens when Python encounters a
defstatement, and what makes the body actually run?View answer
The
defstatement creates a function object and binds it to a name. Its indented body is not executed just because Python sees the definition. To execute the body, call that function with parentheses, such asshow_menu(). This is why a script containing only definitions can finish without printing anything.Why is calling a small named function often better than copying the same five lines into several places?
View answer
A named function places the implementation in one location and makes the calling code express an intention such as
show_menu(). When the behaviour needs changing, you modify one definition instead of finding several copies that might diverge. It is still important to test the function: reuse also means a single mistake can affect every call.
Where this leads.
Now our functions can perform a job. Next we will give them different information each time they run.