Functions in Python

Python 10 min min read Updated: Mar 09, 2026 Intermediate
Functions in Python
Intermediate Topic 1 of 10

Functions in Python

Functions are one of the most important concepts in Python programming. A function is a reusable block of code that performs a specific task. Instead of writing the same code multiple times, we can define a function once and reuse it whenever needed.

Functions help make programs more organized, readable, and maintainable. They are widely used in real-world applications such as data processing, automation scripts, web development, and machine learning programs.

What is a Function?

A function is a block of code that runs only when it is called. Functions can take input values, process them, and return results.

In Python, functions are defined using the def keyword.

python def greet(): print("Hello, welcome to Python!") greet()

In this example, a function named greet() is created and then called to display a message.

Why Use Functions?

  • Reduce code repetition
  • Improve code readability
  • Make programs modular and organized
  • Simplify debugging and maintenance
  • Allow code reuse across different parts of a program

Functions with Parameters

Functions can accept input values called parameters. These parameters allow functions to work with different data.

python def greet_user(name): print("Hello", name) greet_user("Alice") greet_user("Bob")

Here, the function accepts a parameter called name and prints a personalized greeting.

Functions with Return Values

A function can return a result using the return statement.

python def add_numbers(a, b): return a + b result = add_numbers(5, 3) print(result)

The function calculates the sum of two numbers and returns the result.

Default Parameters

Python allows functions to have default parameter values. If no value is provided during the function call, the default value will be used.

python def greet(name="Guest"): print("Hello", name) greet() greet("John")

If no argument is passed, the function uses the default value Guest.

Keyword Arguments

Keyword arguments allow passing parameters by specifying the parameter name during function calls.

python def student_info(name, age): print("Name:", name) print("Age:", age) student_info(age=20, name="Rahul")

This improves code readability and flexibility.

Arbitrary Arguments (*args)

Sometimes we may not know how many arguments will be passed to a function. Python provides *args to handle multiple arguments.

python def total_sum(*numbers): return sum(numbers) print(total_sum(10, 20, 30))

The function accepts any number of arguments and calculates their sum.

Arbitrary Keyword Arguments (**kwargs)

Python also supports passing multiple keyword arguments using **kwargs.

python def student_details(**info): for key, value in info.items(): print(key, ":", value) student_details(name="Alice", age=21, course="Python")

This allows flexible handling of multiple named arguments.

Lambda Functions

Lambda functions are small anonymous functions defined using the lambda keyword. They are typically used for short operations.

python square = lambda x: x * x print(square(5))

Lambda functions are useful in functional programming and when working with functions like map() or filter().

Real-World Example

Functions are widely used in real applications. For example, calculating the area of a circle:

python def calculate_area(radius): pi = 3.14159 return pi * radius * radius area = calculate_area(5) print("Area of circle:", area)

This function calculates and returns the area of a circle.

Best Practices for Writing Functions

  • Use meaningful function names.
  • Keep functions small and focused on one task.
  • Use docstrings to document functions.
  • Avoid overly complex functions.
  • Reuse functions whenever possible.

Conclusion

Functions are a fundamental concept in Python programming that help developers organize code into reusable blocks. By using functions effectively, programs become easier to read, maintain, and expand.

Understanding functions is essential for building larger applications and advanced Python programs.

In the next tutorial, we will explore Recursion in Python and learn how functions can call themselves to solve complex problems.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators