HOME
ABOUT

Functions

Functions

Defining Functions

def greet(name):
    """This function greets the person passed in as parameter"""
    print(f"Hello, {name}!")

greet("Alice")  # Output: Hello, Alice!

Return Values

def add(a, b):
    """Return the sum of two numbers"""
    return a + b

result = add(3, 5)
print(result)  # Output: 8

Default Arguments

def greet(name, message="Good morning!"):
    print(f"Hello {name}, {message}")

greet("Bob")  # Output: Hello Bob, Good morning!
greet("Charlie", "How are you?")  # Output: Hello Charlie, How are you?

Keyword Arguments

def describe_pet(pet_name, animal_type="dog"):
    print(f"I have a {animal_type} named {pet_name}.")

describe_pet(pet_name="Max")
describe_pet(animal_type="hamster", pet_name="Harry")

Variable-length Arguments

def make_pizza(*toppings):
    print("Making pizza with:")
    for topping in toppings:
        print(f"- {topping}")

make_pizza("pepperoni")
make_pizza("mushrooms", "green peppers", "extra cheese")

Related Articles

  • Introduction to Python
  • Setting Up Python Environment
  • Python Syntax Basics
  • Variables and Data Types
  • Operators
  • Input and Output
  • Control Flow: Conditionals
  • Control Flow: Loops
  • Functions
  • Lists and Tuples
  • Dictionaries and Sets
  • String Manipulation
  • How to execute SQL queries (Select, Update, Insert, Delete) effectively using Python?