Python Functions
💡
Exercise 28

D.R.Y. 10 XP Easy

Ctrl+Enter Run Ctrl+S Save

Welcome to Chapter 6: Functions! 🔧

D.R.Y. stands for Don't Repeat Yourself — one of the most important rules in programming!

💡 Think of it like this: A function is like a recipe. You write the recipe once, and then you can use it over and over again. Instead of writing the same code 10 times, you put it in a function and call it 10 times!

# Without functions (BAD — repeating code!): print('Hello, Alice!') print('Welcome to CodeRex!') print('Hello, Bob!') print('Welcome to CodeRex!') # With functions (GOOD — write once, use many!): def greet(name): print(f'Hello, {name}!') print('Welcome to CodeRex!') greet('Alice') greet('Bob')

Key parts of a function:

  • def — the keyword that says "I'm creating a function"
  • Function name — what you call it (use lowercase with underscores)
  • Parameters — values you pass in (inside the parentheses)
  • Body — the indented code inside the function
  • return — optionally send a value back (we'll learn this next!)
def greet(name): # name is a parameter print(f'Hello, {name}!') greet('Alice') # 'Alice' is an argument greet('Bob') # 'Bob' is an argument
📋 Instructions
Create a function called `welcome` that takes a `name` parameter and prints a welcome message. Then call it 3 times with different names: ``` Welcome, Alice! Welcome, Bob! Welcome, Charlie! ```
def welcome(name): then print(f'Welcome, {name}!'). Call it three times. You've got this — take it step by step!
⚠️ Try solving it yourself first — you'll learn more!
# Without functions (BAD — repeating code!):
print('Hello, Alice!')
print('Welcome to CodeRex!')
print('Hello, Bob!')
print('Welcome to CodeRex!')

# With functions (GOOD — write once, use many!):
def greet(name):
    print(f'Hello, {name}!')
    print('Welcome to CodeRex!')

greet('Alice')
greet('Bob')
🧪 Test Cases
Input
Run your code
Expected
Welcome, Alice! Welcome, Bob! Welcome, Charlie!
Expected program output
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.