DEV Community

Lokesh
Lokesh

Posted on

Functions in Python

What Are Functions in Python?

A function is a block of code that performs a specific task. We can create a function once and use it whenever we need it.

In Python, we use the def keyword to define a function.

Basic Syntax of a Function in Python

The basic syntax of a function in Python is:

def function_name(parameters):
    # Function body
    statements
    return value
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • def: Keyword used to define a function.
  • function_name: Name given to the function.
  • parameters: Values that a function can accept (optional).
  • Function body: Contains the statements to be executed.
  • return: Used to send a result back (optional).

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

Here, greet() is the function call that executes the code inside the function.

Why Do We Use Functions in Python?

Functions are useful because:

  1. They help us reuse code.
  2. They reduce code repetition.
  3. They make programs easier to understand.
  4. They save development time.
  5. They make code easier to maintain.

Types of Arguments in Python

Arguments are values passed to a function when we call it. Python supports different ways of passing arguments.

Let's understand the three important types.

1. Positional Arguments

Positional arguments are passed to a function based on their position or order.

Example:

def student(name, age):
    print("Name:", name)
    print("Age:", age)

student("Arun", 21)
Enter fullscreen mode Exit fullscreen mode

Output:

Name: Arun
Age: 21
Enter fullscreen mode Exit fullscreen mode

Here, "Arun" is assigned to name, and 21 is assigned to age.

2. Keyword Arguments

Keyword arguments are passed using parameter names. We don't need to follow the position of the parameters because we specify their names.

Example:

def student(name, age):
    print("Name:", name)
    print("Age:", age)

student(age=21, name="Arun")
Enter fullscreen mode Exit fullscreen mode

Output:

Name: Arun
Age: 21
Enter fullscreen mode Exit fullscreen mode

Here, we pass values using parameter names. So, the order does not matter.

3. Default Arguments

Default arguments are used when we want to assign a default value to a parameter.

If the user does not provide a value, Python automatically uses the default value.

Example:

def student(name, age=20):
    print("Name:", name)
    print("Age:", age)

student("Arun")
student("Priya", 22)
Enter fullscreen mode Exit fullscreen mode

Output:

Name: Arun
Age: 20
Name: Priya
Age: 22
Enter fullscreen mode Exit fullscreen mode

Here, age=20 is the default argument. When we provide only the name, the age is automatically taken as 20. When we provide another age, Python uses that value instead.

What Is the Return Statement in Python?

The return statement is used to send a result from a function back to the place where it was called.

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

Here, the function adds two numbers and returns the result.

Difference Between Positional, Keyword, and Default Arguments

Argument Type Meaning
Positional Values are passed based on order.
Keyword Values are passed using parameter names.
Default A parameter has a predefined value.

Top comments (0)