๐ ๏ธ
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 โ