Functions - Your Code Helpers

8-10 minutes Beginner Level

Understanding the Concept

Functions are like having helpful assistants that do specific jobs for you! Instead of writing the same code over and over, you create a function once and call it whenever you need it. Think of it like a recipe - you write it once, but you can cook the dish many times. Functions make your code organized, reusable, and way less repetitive!

Code Example

# Create a simple function
def greet_user(name):
    print(f"Hello, {name}! Welcome to Python!")
    
# Use (call) your function
greet_user("Alex")
greet_user("Sam")

# Function that returns a value
def add_numbers(a, b):
    result = a + b
    return result

sum_result = add_numbers(5, 3)
print("Sum is:", sum_result)
💡 Explanation: Use 'def' to create functions. They can take inputs (parameters) and give back outputs (return values). Call them by using their name with parentheses!
Quick Actions

Your Turn to Practice

Create a function called 'square_number' that takes a number and returns its square. Then test it with a few different numbers!

Code Sandbox
Ready to run your code! 🚀
Pro tip: Write your solution in the sandbox above and click "Run Code" to test it! Ask our Python tutor if you need help!

Knowledge Check

Key Topics
function def return parameter argument call invoke helper reuse
Learning Path
Getting Started
Variables
Data Types
Python Syntax
Comments
Numbers
Operators
Type Casting
Strings
Booleans
User Input
If...Else
While Loops
For Loops
Lists
Dictionaries
Functions