Your first function
print(), len(), input() — these are all functions: named pieces of code you can call. Now for the exciting part: you’ll build a function of your own.
def — build a function
Section titled “def — build a function”Inputs (for input())
One value per line — read in order by input().
There are two stages:
- Definition:
def greet():— “I’m building a function called greet”. The lines inside it are indented (a familiar rule!). This part does nothing yet — it just writes the recipe. - Call:
greet()— “now run it!”. Each call runs the body of the function once.
Why bother?
Section titled “Why bother?”Instead of writing the same 2 lines 3 times, we wrote them once and called them 3 times. The programmer’s golden rule: don’t repeat yourself! And when you need to change something, you change it in one place — no hunting down every copy.
Missions
Section titled “Missions”Your own command
+10 XP ✓ CompletedBuild a function called greet and call it twice.
Inputs (for input())
One value per line — read in order by input().
Banner function
+15 XP ✓ CompletedTurn the banner trick from Module 01 into a function: when banner() is called, it should print a three-line header.
Inputs (for input())
One value per line — read in order by input().
Next lesson: passing information into a function — parameters. What happens when you write greet("Aysel")? Continue →