Hackforge Academy

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)

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community