Functions in Python

In this tutorial, you will learn how to create Python functions using the def keyword.

What is Function in Python?

A named code block that performs a task or returns a value is referred to as a function.

In some programs, you may need to repeat a task numerous times. You also don’t want to duplicate the code for the same task in several places.

To do so, you encapsulate the code in a function and call it anytime you need to do the operation.

For example, you must use the print() method anytime you want to display a value on the screen. Python displays a value on the screen by running the code inside the print() method.

In practise, functions are used to break down a huge programme into smaller, more manageable chunks. The functions will make it easier to write, read, test, and maintain your programme.

In Python, the print() function is one of many built-in functions. This indicates that certain functions are available across the application.

You’ll learn how to create user-defined Python functions in this article.

How to define a function in Python?

Here’s a quick way to display a greeting:

def greet():
    """ Display a greeting to users """
    print('Hi')

This example demonstrates the most basic function construction. The function definition and body are the two primary components of a function.

Function definition in Python

The def keyword precedes the function name in a function definition (greet).

You must specify information inside the parenthesis if the function requires it to perform its function (). Because the hello function in this example does not require any data, the parentheses are left blank.

A colon always follows the function definition (:).

Function body in Python

The function’s body is made up of all the indented lines that follow the function definition.

A docstring is a text string wrapped by triple quotes. It explains how the function works. Python generates documentation for the function automatically using the docstring.

The single line of actual code in the function body is print(‘Hi’). greet() just has one function: print(‘Hi’).

How to call a function in Python?

You must call a function before you may use it. A function call tells Python to run the code contained within the function.

To call a function, type the name of the function followed by the information it requires in parentheses.

The greet() method is used in the example below. Because the greet() method does not require any input, you must use empty parentheses like this:

greet()

When you start the software, it will display the following greeting:

Hi

How to Pass data to functions in Python?

Assume you want to address users by their first names. To accomplish this, add a name to the function definition in parenthesis like follows:

def greet(name):

A function parameter, or simply parameter, is the name for the name.

You can utilise a parameter added to the function declaration as a variable within the function body:

def greet(name):
    print(f"Hi {name}")

The name parameter is only accessible from within the greet() method, not from outside.

When calling a function with a parameter, you must pass the data. As an example:

greet('John')

Output

Hi John

An argument is the value that you pass into a function. ‘John’ is an argument in this case.

You can also call the method by providing it a variable:

first_name = 'Jane'
greet(first_name)

In this case, the greet() function takes the first name variable as an argument.

What are Parameters and Arguments in Python?

It’s crucial to understand the difference between a function’s arguments and parameters.

A piece of data that a function requires as a parameter. In the function specification, you also define the parameter. A parameter called name, for example, exists in the greet() function.

You give data into the function as an argument. The function argument could be a text string like ‘John’ or a variable like jane.

Parameters and arguments are sometimes used interchangeably.

How to return a value from function n Python?

A function, such as greet(), can accomplish a task. It may also return a value. A return value is the value that a function returns.

The return statement is used inside the function body to return a value from a function.

return value

The greet() function is modified in the following example to return a greeting rather than displaying it on the screen:

def greet(name):
    return f"Hi {name}"

You can add the greet() function’s return value to a variable when you call it:

greeting = greet('John')

And then project it onto the screen:

print(greeting)

Because it is independent of the print() method, the new greet() function is superior to the old.

The greet() function can be reused in different apps afterwards. You can use it to greet people after they check in to a web application, for example.

Passing multiple parameters to functions in Python

There can be zero, one, or more parameters in a function.

The following example defines the sum() function, which computes the sum of two numbers:

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


total = sum(10,20)
print(total)

Output

30

The sum() method in this example takes two parameters, a and b, and returns the sum of them.

When a function has numerous parameters, you must separate them with a comma.

You must supply all of the arguments to the function when calling it. You’ll get an error if you pass the function more or fewer arguments.

Inside the function body, a will be 10 and b will be 20 in the following function call.

total = sum(10, 20)