"Functions in Python are reusable blocks of code that perform specific tasks. They help you organize your code, avoid repetition, and make programs easier to understand and maintain."- Gemini 2025
print("Hello!")
name = "Alice" print(len(name)) # Output: 5
age = 25 print(type(age)) # Output: <class 'int'>
name = input("What is your name? ")
num = int("42") print(num) # Output: 42
A function is a named block of code that you can use over and over. Instead of writing the same code multiple times, you write it once in a function and call it whenever you need it.
A function signature includes the function's name and parameters (the inputs it expects).
Parameters are the variable names in the function definition. Arguments are the actual values you pass when calling the function.
def greet(name): # 'name' is a parameter print(f"Hi, {name}!") greet("Alice") # "Alice" is an argument
Arguments that are matched to parameters based on their position or order.
You can give parameters default values. If no argument is provided, the default is used.
def greet(name, greeting="Hello"): return f"{greeting}, {name}!" print(greet("Bob")) # Uses default print(greet("Bob", "Hi")) # Overrides default
Scope determines where a variable can be used in your program. There are two main types:
def my_function(): local_var = "I'm local" print(local_var) my_function() # Works fine print(local_var) # Error! local_var doesn't exist here
global_var = "I'm global" def my_function(): print(global_var) # Can access global variables my_function() # Works fine print(global_var) # Also works fine
global
counter = 0 def increment(): global counter # Tell Python to use the global variable counter += 1 increment() print(counter) # Output: 1
Functions can send back a result using the return statement. This lets you use the function's output in other parts of your program.
return
def add(a, b): return a + b result = add(5, 3) print(result) # Output: 8
# Simple function with no parameters def say_hello(): print("Hello, world!") say_hello() # Function with one parameter def greet(name): print(f"Hello, {name}!") greet("Alice") greet("Bob") # Function with return value def square(number): return number * number result = square(5) print(result) # Output: 25 # Function with multiple parameters def add(a, b): return a + b total = add(10, 20) print(total) # Output: 30 # Function with default parameter def greet(name, greeting="Hello"): return f"{greeting}, {name}!" print(greet("Alice")) # Output: Hello, Alice! print(greet("Bob", "Hi")) # Output: Hi, Bob! # Function that checks if a number is even def is_even(number): if number % 2 == 0: return True else: return False print(is_even(4)) # Output: True print(is_even(7)) # Output: False # Function to find the larger of two numbers def max_of_two(a, b): if a > b: return a else: return b print(max_of_two(10, 5)) # Output: 10 print(max_of_two(3, 8)) # Output: 8
# Local scope example def calculate(): x = 10 # Local variable y = 5 # Local variable result = x + y return result print(calculate()) # Output: 15 # print(x) # Error! x doesn't exist outside the function # Global scope example price = 100 # Global variable def show_price(): print(f"The price is ${price}") def calculate_tax(): tax = price * 0.08 return tax show_price() # Output: The price is $100 print(calculate_tax()) # Output: 8.0 # Modifying global variables score = 0 # Global variable def add_points(points): global score # Access the global variable score += points add_points(10) print(score) # Output: 10 add_points(5) print(score) # Output: 15 # Local variable shadows global variable name = "Global" # Global variable def print_name(): name = "Local" # Local variable (different from global) print(name) print_name() # Output: Local print(name) # Output: Global (unchanged) # Function calling another function with shared scope def calculate_total(price, quantity): subtotal = price * quantity tax = calculate_tax(subtotal) return subtotal + tax def calculate_tax(amount): tax_rate = 0.08 return amount * tax_rate total = calculate_total(20, 3) print(f"Total: ${total}") # Output: Total: $64.8 # Common scope mistake def broken_function(): # count = 0 # Uncomment this line to fix count += 1 # Error! count not defined return count # broken_function() # This will cause an error # Fixed version with proper initialization def working_function(): count = 0 # Initialize before use count += 1 return count print(working_function()) # Output: 1