Python Function

In this tutorial, we will learn about python function and how to create custom functions in python.

Python Function with Argument

What is a function in Python?

A python function is a set of statements of codes that are organized and reusable that are used to perform related actions, and a function is only run when it is called.

The function is used to break a large code into smaller and modular pieces and reused many times in a program.

Also, functions make the program more readable, organized, and manageable. A function can be both built-in or user-defined in which the user can create its custom functions.

How to define a function in python?

Python functions can be created using the python def keyword followed by the function name and closed by brackets with a colon: and a set of statements inside the function, which we only are executed when we call the function.

Syntax of a user-defined function.

def function_name():
statements1
statements2

 

How to call a function in Python?

A Function can be called or used in a program by calling the function name followed by brackets.

Syntax on how to call a function.

function_name()

Let see an example of how to make a python function.

Example:

def my_function():
print(“Hello Python”)
my_function()

The output will be as follow.

Hello Python

This way, we can create a function in python.

Lets check it function type using type()  method.

Example:

def my_function():
print(“Hello Python”)
print(type(my_function))

Output:

<class ‘function’=“”></class>

As we can see, we got the result as a class function.

Return statement inside the Python Function

The return()  is used to exit the function and return output based on the code implemented with the return() statement.

Syntax of return:

return statement

Let see an example of using the return statement in python.

Example:

def my_function():
return “This is a return statement.”
print(my_function())

Here we are returning a string using a return statement. As we can see, we don’t need to use the print() statement to print the string.

The output will be as follows.

This is a return statement.

We can also add arithmetic operations in the python functions.

Example of arithmetic operations.

def my_function():
x = 2
y = 2
return x+y
print(my_function())

The output will be as follow.

4

 

pass() statement in Python Function

We can use the pass() statement to pass the null statement in the function. When we use the pass() method in python functions, it will not give any output. It will just run an empty statement.

Let see an example of the pass() statement in functions.

def my_function():
pass
print(my_function())

When we run this program, we will get nothing as an output as we are using the pass() statement here.