Category: python
Introduction to Functions
Published on 24 Jul 2026
Explanation
A function is a reusable block of code that performs a specific task. Functions help break large programs into smaller,
Code:
def greet():
print('Welcome to Python Functions!')
print('Functions help reuse code.')
# Function Calls
greet()
greet()
Explanation
Parameters are variables defined in a function declaration, while arguments are the actual values passed when calling the function. Functions
Code:
def display_student(name, age, course):
print('Name:', name)
print('Age:', age)
print('Course:', course)
# Positional Arguments
display_student('John', 22, 'Python')
# Keyword Arguments
display_student(course='Java', age=25, name='Alice')
Explanation
Default parameters allow a function to assign default values to its parameters. If the caller does not provide a value,
Code:
def calculate_bill(amount, tax=18):
total = amount + (amount * tax / 100)
print('Total Bill:', total)
calculate_bill(1000)
calculate_bill(1000, 5)
def employee(name, department='IT'):
print(name, 'works in', department)
employee('Praveen')
employee('John', department='HR')
Explanation
A function can return a value using the return statement. The returned value can be stored in a variable, used
Code:
def add(a, b):
return a + b
result = add(15, 25)
print('Sum:', result)
def calculate(a, b):
return a + b, a - b, a * b
addition, subtraction, multiplication = calculate(20, 5)
print('Addition:', addition)
print('Subtraction:', subtraction)
print('Multiplication:', multiplication)
Explanation
Functions are commonly used to organize business logic into reusable components. For example, a banking application may have functions to
Code:
def calculate_discount(amount):
if amount >= 5000:
return amount * 0.10
return 0
def final_bill(amount):
discount = calculate_discount(amount)
total = amount - discount
print('Purchase Amount:', amount)
print('Discount:', discount)
print('Final Amount:', total)
final_bill(6500)
final_bill(2500)