DEV Community

Mahalakshmi C
Mahalakshmi C

Posted on

Functions in Python

1. What is a Function in Python?

  • A function is a block of reusable code that performs a specific task. Instead of writing the same code repeatedly, we can define a function once and call it whenever needed.

  • Functions help to reduce code repetition, improve code readability, and make programs easier to maintain.

  • Python provides the def keyword to define a function.

Syntax:

def function_name():
    # function body
    statements
Enter fullscreen mode Exit fullscreen mode

Example:

def greet():
    print("Hello, Welcome to Python")

greet()
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, Welcome to Python
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • def is the keyword used to define a function.
  • greet is the function name.
  • The statements inside the function form the function body.
  • greet() is the function call used to execute the function.

2. Why Do We Use Functions?

Functions provide several advantages:

  1. Code Reusability – Write code once and use it multiple times.
  2. Code Readability – Makes the program easier to understand.
  3. Code Organization – Divides a large program into smaller parts.
  4. Easy Maintenance – Changes can be made in one place.
  5. Avoid Code Repetition – Reduces duplicate code.

3. Types of Functions in Python

Python functions can be classified into two main types:

  1. Built-in Functions
  2. User-defined Functions

1. Built-in Functions

Built-in functions are predefined functions provided by Python. We can use them directly without defining them.

Some commonly used built-in functions are:

Function Description
print() Displays output
input() Gets input from the user
len() Returns the length
type() Returns the data type

Example:

numbers = [10, 20, 30, 40]

print(len(numbers))
Enter fullscreen mode Exit fullscreen mode

Output:

4
Enter fullscreen mode Exit fullscreen mode

2. User-defined Functions

User-defined functions are functions created by programmers to perform specific tasks.

The def keyword is used to create user-defined functions.

Example:

def add():
    a = 10
    b = 20
    print(a + b)

add()
Enter fullscreen mode Exit fullscreen mode

Output:

30
Enter fullscreen mode Exit fullscreen mode

4. Types of User-defined Functions Based on Arguments and Return Values

Based on arguments and return values, functions can be divided into four types.

1. Function Without Arguments and Without Return Value

This function does not accept any arguments and does not return any value.

Example:

def display():
    print("Welcome to Python")

display()
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to Python
Enter fullscreen mode Exit fullscreen mode

2. Function With Arguments and Without Return Value

This function accepts arguments but does not return any value.

Example:

def add(a, b):
    print(a + b)

add(10, 20)
Enter fullscreen mode Exit fullscreen mode

Output:

30
Enter fullscreen mode Exit fullscreen mode

Here, a and b receive the values passed during the function call.

3. Function Without Arguments and With Return Value

This function does not accept arguments but returns a value using the return keyword.

Example:

def add():
    a = 10
    b = 20
    return a + b

result = add()
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

30
Enter fullscreen mode Exit fullscreen mode

Here, the function calculates the result and sends it back to the caller using return.

4. Function With Arguments and With Return Value

This function accepts arguments and returns a result.

Example:

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

result = add(10, 20)
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

30
Enter fullscreen mode Exit fullscreen mode

This is one of the most commonly used function types in Python.

Conclusion

Functions are an important concept in Python programming. They help developers write reusable, organized, and maintainable code.

Understanding function definitions, arguments, parameters, return statements provides a strong foundation for developing Python applications.

Top comments (0)