L3·Functions & Modules

🧩Defining Functions

def, return, default arguments, docstrings.

defreturndocstring

§ 1def

def name(params): body. Return with return; without it, returns None.

§ 2Default arguments

Evaluated once at definition. Never use mutable defaults like [] or {} — use None + assign inside.

§ 3Docstrings

First string in the body becomes .__doc__. Show up in help().

Worked example

1def greet(name, greeting="Hello"):​
2 """Return a friendly greeting."""​
3 return f"{greeting}, {name}!"​
4​
5print(greet("Ada"))​
6print(greet("Bob", greeting="Hi"))​

Try it live · Python runs in your browser

Loading playground…

Checkpoint quiz

Question 1 / 2
What does 'return' with no value give?

Lesson test

Take an 8-question test on “Defining Functions” — concepts, output prediction, bug spotting and interview questions.