Home Course Roadmap
Lesson 29 · Functions

Python Parameters

Learn how to make Python functions flexible and reusable by passing information through parameters and arguments.

40–50 minutes Beginner Functions
0%

What You'll Learn

  • Understand parameters and arguments.
  • Use positional arguments.
  • Use keyword arguments.
  • Create functions with multiple parameters.
  • Use default parameter values.
  • Understand parameter order and rules.
  • Pass variables, expressions, lists, and dictionaries.
  • Build flexible real-world functions.
01

What Are Parameters?

A parameter is a variable defined inside a function's parentheses. Parameters allow a function to receive information from the code that calls it.

Without parameters, a function may perform exactly the same operation every time. Parameters make functions flexible.

Basic Parameter
def greet(name):
    print(f"Hello, {name}!")

greet("Ali")
greet("Sara")
Output
Hello, Ali!
Hello, Sara!

The function is the same, but the result changes because different values are supplied.

02

Parameters vs Arguments

These two terms are closely related, but they describe different things.

Parameter

A variable written in the function definition.

Argument

The actual value supplied when the function is called.

Example
def greet(name):
    print(f"Hello, {name}!")

greet("Adnan")

Here, name is the parameter and "Adnan" is the argument.

03

Positional Arguments

With positional arguments, values are assigned to parameters according to their position.

Positional Arguments
def introduce(name, age):
    print(f"Name: {name}")
    print(f"Age: {age}")

introduce("Ali", 25)
Output
Name: Ali
Age: 25

Python matches "Ali" with name and 25 with age.

Order matters:

If you reverse the arguments, the values will be assigned to the wrong parameters.

04

Keyword Arguments

A keyword argument explicitly specifies which parameter should receive a value.

Keyword Arguments
def introduce(name, age):
    print(f"Name: {name}")
    print(f"Age: {age}")

introduce(age=25, name="Ali")
Output
Name: Ali
Age: 25

Notice that the arguments were supplied in a different order, but Python still assigned them correctly because their parameter names were explicitly specified.

05

Using Multiple Parameters

A function can have several parameters.

Multiple Parameters
def calculate_bill(item, price, quantity):
    total = price * quantity

    print(f"Item: {item}")
    print(f"Total: Rs. {total}")

calculate_bill("Laptop Bag", 2500, 2)
Output
Item: Laptop Bag
Total: Rs. 5000

Each parameter represents a piece of information required by the function.

06

Default Parameters

A parameter can have a default value. If the caller does not provide a value, Python uses the default.

Default Value
def greet(name="Student"):
    print(f"Hello, {name}!")

greet()
greet("Ahmed")
Output
Hello, Student!
Hello, Ahmed!

Default parameters are useful when a value is optional or when there is a sensible standard value.

Practical Example
def create_profile(name, country="Pakistan"):
    print(f"Name: {name}")
    print(f"Country: {country}")

create_profile("Ali")
create_profile("Sara", "Canada")
07

Required Parameters

A parameter without a default value is normally required.

Required Parameter
def add(a, b):
    return a + b

print(add(10, 20))

Both a and b must receive values.

Missing argument:

Calling add(10) causes Python to raise a TypeError because b has not received a value.

08

Understanding Parameter Order

Python follows specific rules when parameters are defined.

In a simple function, required parameters normally come before parameters that have default values.

Correct
def create_user(name, country="Pakistan"):
    print(name, country)
Important rule:

A parameter with a default value cannot normally be placed before a required positional parameter in the same parameter list.

Incorrect
def create_user(country="Pakistan", name):
    print(name, country)

Python will raise a SyntaxError for this definition.

09

Combining Positional and Keyword Arguments

Python allows positional and keyword arguments to be used in the same function call, but positional arguments must come before keyword arguments.

Valid Combination
def student(name, age, city):
    print(name, age, city)

student("Ali", age=22, city="Multan")
Output
Ali 22 Multan
Remember:

Once you start supplying arguments by keyword, you cannot place another positional argument after them.

10

Expressions as Arguments

An argument does not have to be a literal value. You can pass an expression.

Expressions
def double(number):
    return number * 2

result = double(10 + 5)

print(result)
Output
30

Python evaluates 10 + 5 first and then passes the resulting value to the function.

11

Passing Variables as Arguments

Variables can be passed directly to functions.

Variables as Arguments
def calculate_total(price, quantity):
    return price * quantity

price = 1500
quantity = 4

total = calculate_total(price, quantity)

print(total)
Output
6000

This pattern is extremely common in real applications because data usually comes from variables, user input, databases, or APIs.

12

Passing Lists and Dictionaries

Functions can receive complex data structures such as lists and dictionaries.

Passing a list

List Parameter
def show_students(students):
    for student in students:
        print(student)

names = ["Ali", "Sara", "Ahmed"]

show_students(names)
Output
Ali
Sara
Ahmed

Passing a dictionary

Dictionary Parameter
def show_profile(profile):
    print(f"Name: {profile['name']}")
    print(f"Age: {profile['age']}")

user = {
    "name": "Ali",
    "age": 25
}

show_profile(user)

Functions can therefore operate on complete collections of data rather than only individual values.

13

Using None as a Default

Sometimes you need a parameter whose default value represents "no value supplied."

Python commonly uses None for this purpose.

None as Default
def welcome(name=None):

    if name is None:
        print("Welcome, Guest!")
    else:
        print(f"Welcome, {name}!")

welcome()
welcome("Adnan")
Output
Welcome, Guest!
Welcome, Adnan!

Good Practice

When checking specifically for the absence of a value, use is None rather than == None.

14

Parameters + Return Values

Parameters become especially powerful when combined with return values.

Calculation Function
def calculate_discount(price, percentage):
    discount = price * percentage / 100
    final_price = price - discount

    return final_price

total = calculate_discount(5000, 10)

print(f"Final Price: Rs. {total}")
Output
Final Price: Rs. 4500.0

The parameters provide the input, the function performs the processing, and return provides the result.

Think of the pattern like this:

Arguments → Parameters → Processing → Return Value

15

Real-World Examples

Example 1 — Ecommerce Order

Order Calculator
def calculate_order(product, price, quantity=1):
    total = price * quantity

    return {
        "product": product,
        "quantity": quantity,
        "total": total
    }

order = calculate_order("Shoes", 4500, 2)

print(order)
Output
{'product': 'Shoes', 'quantity': 2, 'total': 9000}

Example 2 — Employee Salary

Salary Calculator
def calculate_salary(basic, allowance=5000):
    return basic + allowance

salary = calculate_salary(50000)

print(f"Salary: Rs. {salary}")
Output
Salary: Rs. 55000

Example 3 — Temperature Converter

Temperature Function
def celsius_to_fahrenheit(celsius):
    return (celsius * 9 / 5) + 32

temperature = celsius_to_fahrenheit(30)

print(f"Temperature: {temperature}°F")
Output
Temperature: 86.0°F
16

Common Parameter Mistakes

1. Providing too few arguments

Problem
def add(a, b):
    return a + b

add(10)

Python raises a TypeError because b is missing.

2. Providing too many arguments

Problem
def add(a, b):
    return a + b

add(10, 20, 30)

The function accepts two parameters, but three arguments were supplied.

3. Positional argument after keyword argument

Problem
def student(name, age):
    print(name, age)

student(name="Ali", 25)

This produces a SyntaxError. Positional arguments must come before keyword arguments.

4. Repeating the same argument

Problem
def student(name, age):
    print(name, age)

student("Ali", name="Ahmed", age=25)

The parameter name receives more than one value, which causes a TypeError.

5. Confusing parameter names

Best practice:

Use clear parameter names such as price, quantity, student_name, or customer_email.

17

Practice Exercises

Exercise 1 — Greeting

Create a function that accepts a name and prints a personalized greeting.

Exercise 2 — Addition

Create a function that accepts two numbers and returns their sum.

Exercise 3 — Rectangle

Create a function with length and width parameters that returns the area.

Exercise 4 — Default Country

Create a function with country="Pakistan" as a default parameter.

Exercise 5 — Discount

Create a function that accepts price and discount percentage and returns the final price.

Exercise 6 — Student Profile

Create a function that accepts a student's name, age, and city and displays the information.

Exercise 7 — List Average

Create a function that receives a list of numbers and returns the average.

18

Mini Challenge — Sales Invoice Generator

Build a small reusable sales invoice system using functions and parameters.

🎯 Your Mission

Create a function that receives:

  • Product name
  • Price
  • Quantity
  • Discount percentage

The function should calculate the subtotal, discount, and final amount.

Suggested Solution
def create_invoice(product, price, quantity=1, discount=0):

    subtotal = price * quantity

    discount_amount = subtotal * discount / 100

    final_amount = subtotal - discount_amount

    print("----- INVOICE -----")
    print(f"Product: {product}")
    print(f"Price: Rs. {price}")
    print(f"Quantity: {quantity}")
    print(f"Subtotal: Rs. {subtotal}")
    print(f"Discount: Rs. {discount_amount}")
    print(f"Final Amount: Rs. {final_amount}")


create_invoice(
    "Python Course",
    5000,
    quantity=2,
    discount=10
)
Expected Output
----- INVOICE -----
Product: Python Course
Price: Rs. 5000
Quantity: 2
Subtotal: Rs. 10000
Discount: Rs. 1000.0
Final Amount: Rs. 9000.0
Challenge extension:

Add a tax parameter and calculate the final amount after both discount and tax.

19

Quick Knowledge Check

1. What is a parameter?

2. What is an argument?

3. Which type of argument uses parameter names?

4. What happens if a required argument is missing?

5. Which is a valid default parameter?

6. Which argument order is valid?

7. Which value commonly represents "no value"?

8. Which is the best check for None?

20

Lesson Summary

Parameters make functions flexible by allowing them to receive information from the code that calls them.

Key Takeaways

  • Parameters are variables defined by a function.
  • Arguments are values supplied during a function call.
  • Positional arguments are matched according to order.
  • Keyword arguments explicitly identify parameters.
  • Functions can have multiple parameters.
  • Default parameters provide fallback values.
  • Required parameters must receive values.
  • Positional arguments must come before keyword arguments.
  • Lists and dictionaries can be passed to functions.
  • None is commonly used to represent the absence of a value.

You now have a much stronger understanding of how information moves into functions. In the next lesson, we will focus specifically on one of the most important parts of functions: return values.

Ready for Lesson 30?

Mark this lesson complete when you are comfortable using positional arguments, keyword arguments, and default parameters.