Python Functions
Python Functions is a block of statements that return the specific
task. The idea is to put some commonly or repeatedly done tasks
together and make a function so that instead of writing the same
code again and again for different inputs, we can do the function
calls to reuse code contained in it over and over again.
Some Benefits of Using Functions
Increase Code Readability
Increase Code Reusability
Python Function Declaration
The syntax to declare a function is:
Syntax of Python Function Declaration
Types of Functions in Python
There are mainly two types of functions in Python.
Built-in library function: These are Standard functions in
Python that are available to use.
User-defined function: We can create our own functions
based on our requirements.
Creating a Function in Python
We can define a function in Python, using the def keyword. We can
add any type of functionalities and properties to it as we require.
Python3
# A simple Python function
def fun():
print("Welcome to GFG")
Calling a Python Function
After creating a function in Python we can call it by using the name
of the function followed by parenthesis containing parameters of
that particular function.
, Python3
# A simple Python function
def fun():
print("Welcome to GFG")
# Driver code to call a function
fun()
Output:
Welcome to GFG
Python Function with Parameters
If you have experience in C/C++ or Java then you must be thinking
about the return type of the function and data type of arguments.
That is possible in Python as well (specifically for Python 3.5 and
above).
Defining and calling a function with parameters
def function_name(parameter: data_type) -> return_type:
"""Docstring"""
# body of the function
return expression
The following example uses arguments and parameters that you will
learn later in this article so you can come back to it again if not
understood.
Python3
def add(num1: int, num2: int) -> int:
"""Add two numbers"""
num3 = num1 + num2
return num3
# Driver code
num1, num2 = 5, 15
ans = add(num1, num2)
print(f"The addition of {num1} and {num2} results {ans}.")
Output:
The addition of 5 and 15 results 20.
Note: The following examples are defined using syntax 1, try to
convert them in syntax 2 for practice.
, Python3
# some more functions
def is_prime(n):
if n in [2, 3]:
return True
if (n == 1) or (n % 2 == 0):
return False
r = 3
while r * r <= n:
if n % r == 0:
return False
r += 2
return True
print(is_prime(78), is_prime(79))
Output:
False True
Python Function Arguments
Arguments are the values passed inside the parenthesis of the
function. A function can have any number of arguments separated
by a comma.
In this example, we will create a simple function in Python to check
whether the number passed as an argument to the function is even
or odd.
Python3
# A simple Python function to check
# whether x is even or odd
def evenOdd(x):
if (x % 2 == 0):
print("even")
else:
print("odd")
# Driver code to call the function
evenOdd(2)
evenOdd(3)
Output:
even
odd