Python Day 4: Python Functions

1. Defining and Calling a Function

# Defining a simple function
def greet():
    print("Hello, welcome to Python functions!")

# Calling the function
greet()

# Output: Hello, welcome to Python functions!

2. Function with Parameters

# Function with parameters
def greet(name):
    print(f"Hello, {name}!")

# Calling the function with an argument
greet("Alice")

# Output: Hello, Alice!

3. Return Values

# Function with a return value
def add(a, b):
    return a + b

# Calling the function and storing the result
result = add(5, 3)
print(result)

# Output: 8

4. Default Parameters

# Function with a default parameter
def greet(name="Guest"):
    print(f"Hello, {name}!")

# Calling function without argument
greet()

# Calling function with an argument
greet("Bob")

# Output: 
# Hello, Guest!
# Hello, Bob!

5. Lambda Functions

# Using a lambda function
square = lambda x: x * x

# Calling the lambda function
print(square(5))

# Output: 25

6. Function with Multiple Return Values

# Function returning multiple values
def get_min_max(numbers):
    return min(numbers), max(numbers)

# Calling the function
minimum, maximum = get_min_max([4, 7, 2, 8, 1])
print(f"Min: {minimum}, Max: {maximum}")

# Output: Min: 1, Max: 8