🐍
PYTHON QUESTAgent Academy
0 XP
150 XP to Next Rank
Mission #9

⚡Lesson 9: Functions -- Your Custom Superpowers

Create your own reusable code blocks!

Lesson 9: Functions -- Your Custom Superpowers

You have already used built-in Python functions like print() and len(). With functions, you can bundle your own code into custom named spells that you can reuse anytime.

Key Concepts to Remember:

  • •
    The def Keyword: Teaches Python a new recipe: def cast_spell(name):.
  • •
    Parameters (Ingredients): Variables passed inside the parentheses for the function to work with.
  • •
    The return Statement: Hands back a calculated answer to wherever the function was called.
  • •
    Calling the Function: Defining a function teaches Python the spell; you must call it with () to cast it.

💻Defining and Calling Custom Spells (IDE Syntax Colors)

def calculate_damage(level):
return level * 15
 
power = calculate_damage(5)
print(f"Spell dealt {power} damage!")

💡 calculate_damage takes level 5, multiplies by 15, and returns 75.

💡 Pro Tip: Do not forget the colon (:) at the end of the def line and indentation on the lines inside.