When you're just starting out with Python, you'll quickly notice that writing the same code again and again becomes tedious. That’s where functions come into play. They allow you to write a block of code once and reuse it whenever needed. It’s like packing your daily routine into a neat, reusable box that you can open whenever required.

In this blog, we’ll break down what functions are, how to define them, the syntax involved, and how they make your code efficient and organized. You’ll also get practical examples and tips that’ll help you become more confident in writing clean Python code.
If you're new to Python or looking to build a solid foundation in programming, check out Uncodemy’s Python Course designed to make learning interactive and career-focused.
A function in Python is a block of organized, reusable code used to perform a specific task. Functions help reduce code repetition, improve readability, and make debugging easier.
There are two types of functions in Python:
Let’s say you’re working on a calculator app. You’ll often need to add, subtract, multiply, or divide numbers. Instead of writing the logic for each operation multiple times, you can write a function for each and call them as needed.
Benefits:
The basic syntax of defining a function in Python is:
Copy Code
def function_name(parameters):
"""docstring (optional)"""
# code block
return valueLet’s break it down:
Copy Code
def greet():
print("Hello, welcome to Python learning!")
greet()Output:
Hello, welcome to Python learning!
Copy Code
def add(a, b):
return a + b
result = add(5, 3)
print("The sum is:", result)Output:
The sum is: 8
Copy Code
def greet(name="Guest"):
print(f"Hello, {name}!")
greet()
greet("Muskan")Output:
Hello, Guest!
Hello, Muskan!
Copy Code
The return statement is used when you want your function to give back a result.
def square(num):
return num ** 2
print(square(4))Output:
16
It’s always a good practice to write a docstring right after the function definition to explain what it does.
Copy Code
def multiply(a, b):
"""Returns the product of two numbers."""
return a * b
You can then use the help() function to view it:
help(multiply)Positional Arguments
These are arguments passed to functions in the correct position.
Copy Code
def full_name(first, last):
print(f"{first} {last}")
full_name("Muskan", "Singh")These are explicitly named when calling the function.
Copy Code
full_name(last="Singh", first="Muskan")
Sometimes you don’t know in advance how many arguments will be passed.
Copy Code
def total_sum(*numbers):
return sum(numbers)
print(total_sum(1, 2, 3, 4))Copy Code
def display_info(**info):
for key, value in info.items():
print(f"{key}: {value}")
display_info(name="Muskan", age=20)Variables defined inside a function are local to that function.
Copy Code
def show_name():
name = "Python"
print(name)
show_name()
# print(name) # This will throw an errorLambda functions are small, one-line functions without a name.
square = lambda x: x ** 2
print(square(5))
Output:
25
A function that calls itself is called a recursive function.
Copy Code
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))Output:
120
Copy Code
def calculate_total(bill_amount, tax_rate=0.18):
"""Calculate final bill including tax"""
return bill_amount + (bill_amount * tax_rate)
print("Total bill is:", calculate_total(1000))Understanding how to define functions in Python is fundamental to writing efficient and readable code. As you begin creating larger projects, you’ll notice how functions act as the building blocks of your applications. From calculators to web apps, everything relies on well-structured function logic.
If you're eager to take your Python skills to the next level, don’t miss the Python Programming Course by Uncodemy. With real-world projects, expert guidance, and practical learning you're on your way to becoming job-ready in no time.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR