DEV Community

Vasanthakumari N
Vasanthakumari N

Posted on Edited on

Python Day 5: Understanding Functions in Python

What is a Function?

A function is a block of code or a set of instructions used to perform a specific task. Every function has a name, and we can call that name whenever we want to execute the code inside it.

In simple words, a function is a reusable block of code.

Example 1: Attending a Class

When we attend a class, we follow a set of steps:

  1. Get ready for class.
  2. Take a laptop.
  3. Take a notebook and pen.
  4. Take a headset.
  5. Join the class.

Example 2: Playing Cricket

When we plan to play cricket, we follow some steps:

  1. Invite friends.
  2. Take a bat and ball.
  3. Go to the ground.
  4. Toss.
  5. Start batting or fielding.

Similarly, in programming, we can group a set of instructions into a function and execute them whenever needed.

How to Define a Function in Python?

To create a function in Python, we use the def keyword.

def means definition.

Syntax:

def function_name():
# Set of instructions

Example:

def play():
    print("Calling friends")
    print("Taking necessary items")
    print("Going to ground")
    print("Playing cricket")
Enter fullscreen mode Exit fullscreen mode

Here:

  • def is the keyword used to define a function.
  • play is the function name.
  • () represents the function's parameter list.
  • The indented statements are the function body.

How to Call a Function?

Defining a function does not execute its code automatically.

To execute it, we need to call the function using its name followed by parentheses.

def play():
    print("Calling friends")
    print("Taking necessary items")
    print("Going to ground")
    print("Playing cricket")

play()
Enter fullscreen mode Exit fullscreen mode
Output:

Calling friends
Taking necessary items
Going to ground
Playing cricket
Enter fullscreen mode Exit fullscreen mode

Why Do We Need Functions?

Functions are useful for several reasons:

  • Code Reusability: We can write code once and use it multiple times.
  • Easy Maintenance: If we need to change something, we can update the function in one place.
  • Code Readability: Functions make code easier to understand.
  • Modularity: We can divide a large program into smaller, manageable parts.
  • Easy Testing and Debugging: We can test individual functions and find errors more easily.

Multiple Functions in Python

We can define multiple functions in a single Python file and call whichever function we need.

Example:

def add():
    print(5 + 10)

def subtract():
    print(10 - 5)

def multiply():
    print(10 * 2)

def divide():
    print(100 / 2)

add()
subtract()
multiply()
divide()

Enter fullscreen mode Exit fullscreen mode
Output:

15
5
20
50.0
Enter fullscreen mode Exit fullscreen mode

Important points:

  • We can define multiple functions in the same Python file.
  • We can call any defined function whenever required.
  • A function must be defined before it is called during program execution.
  • If we define two functions with the same name, the latest definition replaces the earlier one.

Example:

def welcome():
    print("Hello")

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

welcome()
Enter fullscreen mode Exit fullscreen mode
Output:

Welcome to Python
Enter fullscreen mode Exit fullscreen mode

The second function definition replaces the first one because both functions have the same name.

Predefined Functions in Python

Python provides many built-in functions that we can use directly without defining them ourselves.

print(),bin(),len(),type(),input()

Example:

print("Good Morning")

print(bin(4))  # 0b100
print(bin(5))  # 0b101
Enter fullscreen mode Exit fullscreen mode

Output:

Good Morning
0b100
0b101
Enter fullscreen mode Exit fullscreen mode

Parameters and Arguments

Parameters and arguments are important concepts when working with functions.

  • Parameters: Variables mentioned inside the function definition.
  • Arguments: Actual values passed to a function when calling it.

Example:

def add(no1, no2):
    print(no1 + no2)

add(100, 200)
Enter fullscreen mode Exit fullscreen mode

Here:

  • no1 and no2 are parameters.
  • 100 and 200 are arguments.
Output:

300
Enter fullscreen mode Exit fullscreen mode

Function Overloading

Function overloading generally means using the same function name with different numbers or types of arguments.

For example, in some programming languages, we can create multiple functions with the same name but different parameter lists.

However, Python does not support traditional function overloading.

If we define multiple functions with the same name, the latest definition replaces the previous one.

We can achieve similar flexibility in Python using default arguments or variable-length arguments.

References

Top comments (0)