PYTHON BASICS • LESSON 17

Function Arguments & Scope

Learn how Python functions receive information using positional arguments, keyword arguments, default parameters, and flexible arguments. You will also learn how variable scope works inside and outside functions.

1. Positional Arguments

When arguments are passed according to the order of parameters, they are called positional arguments.

def introduce(name, age):
    print(name, age)

introduce("Rahul", 22)

"Rahul" goes to name and 22 goes to age.

CHECK YOUR UNDERSTANDING

In introduce("Rahul", 22), which value goes to age?

2. Keyword Arguments

Keyword arguments explicitly specify which parameter should receive each value.

def introduce(name, age):
    print(name, age)

introduce(age=22, name="Rahul")

Here the order does not matter because the parameter names are specified.

CHECK YOUR UNDERSTANDING

What type of arguments are used in introduce(age=22, name="Rahul")?

3. Predict the Output

def calculate(a, b):
    return a - b

print(calculate(b=20, a=50))

What will Python print?

CHECK YOUR UNDERSTANDING

What is the output?

4. Default Arguments

A function parameter can have a default value. Python uses that value when the caller does not provide an argument.

def calculate_tax(amount, rate=0.10):
    return amount * rate

print(calculate_tax(10000))
print(calculate_tax(10000, 0.05))

CHECK YOUR UNDERSTANDING

What tax rate is used by calculate_tax(10000)?

Fill in the Blank

Complete the default parameter: def tax(amount, rate=___):

5. Multiple Arguments

Functions can accept several parameters.

def order_total(price, quantity, discount):
    subtotal = price * quantity
    return subtotal - discount

total = order_total(1000, 5, 500)
print(total)

CHECK YOUR UNDERSTANDING

What is the order total?

6. Flexible Arguments with *args

*args allows a function to receive multiple positional arguments.

def total_sales(*sales):
    return sum(sales)

print(total_sales(100, 200, 300))

The values are collected into a tuple-like collection inside the function.

CHECK YOUR UNDERSTANDING

What does *args allow a function to receive?

CHECK YOUR UNDERSTANDING

What is the output of total_sales(100, 200, 300)?

7. Flexible Keyword Arguments with **kwargs

**kwargs allows a function to receive multiple keyword arguments.

def show_profile(**details):
    print(details)

show_profile(name="Aman", city="Dehradun")

The keyword arguments are collected into a dictionary.

CHECK YOUR UNDERSTANDING

What type of structure does **kwargs collect keyword arguments into?

8. Understanding Variable Scope

Scope determines where a variable can be accessed.

A variable created inside a function is generally local to that function.

def calculate():
    sales = 50000
    return sales

print(calculate())

CHECK YOUR UNDERSTANDING

Where is the variable sales created in the example?

9. Local vs Global Variables

company = "Vista Academy"

def show_company():
    print(company)

show_company()

The function can access the variable company because it exists outside the function.

CHECK YOUR UNDERSTANDING

What will show_company() print?

Fill in the Blank

A variable created inside a function is usually called a ___ variable.

10. Functions for Data Analytics

Functions become especially useful when the same analytical calculation must be repeated across many records.

def profit_margin(revenue, profit):
    return (profit / revenue) * 100

margin = profit_margin(200000, 50000)
print(margin)

The function calculates profit margin as a percentage.

CHECK YOUR UNDERSTANDING

What is the profit margin when revenue is ₹200,000 and profit is ₹50,000?

11. Debugging Function Arguments

Find the problem:

def multiply(a, b):
    return a * b

print(multiply(5))

CHECK YOUR UNDERSTANDING

Why does multiply(5) cause a problem?

THINK LIKE AN ANALYST

Design a Reusable Function

Imagine you have revenue and cost values for hundreds of transactions. Instead of writing the profit calculation again and again, create one function and reuse it.

def calculate_profit(revenue, cost):
    return revenue - cost

CHECK YOUR UNDERSTANDING

Which call correctly calculates profit for revenue ₹120,000 and cost ₹80,000?

CHECK YOUR UNDERSTANDING

Which statement best describes local scope?

FINAL CHALLENGE

Build a Sales Analysis Function

Create a function that accepts sales amount and target amount. It should return the percentage of target achieved.

def target_percentage(sales, target):
    return (sales / target) * 100

What percentage is achieved when sales = ₹80,000 and target = ₹100,000?

CHECK YOUR UNDERSTANDING

What is the target achievement percentage?

Lesson 17 Recap

  • ✓ Positional arguments are matched by order.
  • ✓ Keyword arguments identify parameters by name.
  • ✓ Default arguments provide fallback values.
  • ✓ *args collects multiple positional arguments.
  • ✓ **kwargs collects multiple keyword arguments.
  • ✓ Local variables belong to their function scope.
  • ✓ Functions make repeated analytics calculations reusable.