RisingCoder Full course โ†’
Day 1Day 2Day 3Day 4
๐Ÿ› ๏ธ
STUDENT NOTES ยท DAY 9 OF 24

Build Your Own Spells ๐Ÿ› ๏ธ

print() and len() are spells someone else wrote. Today YOU write spells โ€” called functions.

1def โ€” define a spell

๐Ÿ
Py: def writes the recipe. Nothing cooks until you CALL it: greet("Sana"). Recipe first, dinner later.

2return โ€” the spell sends something BACK

The Function Machine: double(x)

double(5) โ†’ ๐Ÿ› ๏ธ โ†’ 10
๐Ÿค–
Bittu: A function is a vending machine: coin in (argument), snack out (return value).

3The mystery of None

๐Ÿž
Bugly: What if the function has NO return? What comes back? Heheheโ€ฆ
๐Ÿฆ‰
Professor Hoot: No return โ†’ the function quietly hands back None ("nothing"). It shouted, but returned nothing. Common beginner surprise!

4Default values โ€” the spell's standby mode

5Quick quiz! ๐Ÿง 

Which keyword creates a function?
๐Ÿ’ก def, short for define. Python keeps ceremonies short.
def f(x): print(x) โ€” what does y = f(3) store in y?
๐Ÿ’ก It PRINTS 3, but with no return statement it hands back None.

6Your mission ๐Ÿš€

Mission 1: Write cheer(name) that prints "Go name go! ๐Ÿ“ฃ" โ€” call it for 3 friends.
Mission 2: Write square(x) that RETURNS x*x. Print square(7).
Mission 3: Write greet(name, lang="en") that says Hello for en and Namaste for hi.

Day 9 done โ€” spellwright! ๐Ÿ› ๏ธ

Next: *args, **kwargs and one-line lambda spells.

Go to Day 10: Advanced Functions โ†’

๐ŸŽฎ Or test yourself in the Python Arcade โ†’