Lesson 21 of 34

Functions: Your Own Commands

8 min 25 XP

Writing the same code again and again? Make it a function. Start with def, then a name, ( ) and :, and indent the code inside.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

Put parameters inside ( ). The values you pass when calling the function (arguments) arrive in them.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

Defining a function (def) doesn't run it. You have to call it: greet(...)

Challenge

+25 XP

Write a function greet(name) that prints "Vanakkam, <name>!". Then call greet("Arun") and greet("Priya").

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.