Skip to content

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.

Inputs (for input())

One value per line — read in order by input().

Output
 

There are two stages:

  1. 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.
  2. Call: greet()“now run it!”. Each call runs the body of the function once.

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.

Mission

Your own command

+10 XP

Build a function called greet and call it twice.

Inputs (for input())

One value per line — read in order by input().

Output
 
Mission

Banner function

+15 XP

Turn 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().

Output
 

Next lesson: passing information into a function — parameters. What happens when you write greet("Aysel")? Continue →