Function in Python

By: On:

In this python tutorial, you will learn about function in python with a few examples. Here we will check:

  • What is a function in Python
  • How to create a function in python
  • How to call a function in python
  • Arguments in a python function
  • Python arbitrary arguments
  • Python keyword arguments
  • Default parameter value
  • Passing a list as an argument in python
  • The return value in python

What is a function in Python

What is a function in Python? Function in python provides reusability, organized, and modular code to perform a specific action. The function makes the code easier by breaking the program into smaller chunks and it avoids repetition and makes the code reusable. A function only runs when it is called.


Python has many built-in functions. We can also create our own function to do more specific jobs, and it is called a user-defined function. You can check out Python built-in functions with examples.

How to create a function in Python

Let us see how to create a function in Python?

Using the def statement is the most common way to define a function in python. Here, the parameter is an optional list of identifiers that gets bound to the value supplied as arguments separated by commas.

Example:


def function(parameters):
print("Welcome to function")

How to call a function in Python

Now, let us see how to call a function in Python? To call a function Python, we will use the function name followed by a parenthesis.

Example:

def new_function():
print("Welcome to function")
new_function()

After writing the above code (calling a function in python), Ones you will call “new_function()” then the output will appear as a “ Welcome to function “. Here, we call the function by its function name with parameters.

You can refer to the below screenshot for calling a function in python.

Calling a function in python
Calling a function in python

Python function arguments

In python, information is passed into functions as arguments. Arguments are passed inside the parenthesis and we can add as many arguments, but it should be separated with commas. Here, will see a single argument “ stu_name “.

Example:

def new_function(stu_name):
print(stu_name + " good ")
new_function("James")
new_function("Kavish")

After writing the above code (arguments in python), Ones you will call “new_function()” then the output will appear as a “ James good Kavish good “. Here, when the function is called we pass the student name which is used inside the function and it will print with “good”.

You can refer to the below screenshot arguments in python.

Arguments in python
Arguments in python

Here, we will see the function with two arguments and the function must be called with the correct number of arguments to avoid an error.

Example:

def new_function(name, behaviour):
print(name + " " + behaviour)
new_function("James", "good")

After writing the above code (function with two arguments in python), Ones you will call “new_function()” then the output will appear as a “ James good “. Here, the function is called with two arguments as it expects two arguments.

You can refer to the below screenshot function with two arguments in python.

python function arguments
python function arguments

Python arbitrary arguments

In python, if we don’t know in advance that how many numbers of arguments will be passed into the function then we use the asterisk “(*)” before the parameter name in the function.

Example:

def new_function(*food):
for foods in food:
print("Favorite", foods)
new_function("pizza", "pasta", "maggi")

After writing the above code (python arbitrary arguments), Ones you will call “new_function()” then the output will appear as a “ Favorite pizza Favorite pasta Favorite maggi “. Here, we called the function with multiple arguments and inside the function, we used the for loop to retrieve all arguments.

You can refer to the below screenshot python arbitrary arguments.

Python arbitrary arguments
Python arbitrary arguments

Python keyword arguments

A keyword argument is an argument passed to a function with “keyword = value”. Here, the keyword is the keyword argument and value is the object passed.

Example:

def new_function(student1, student2):
print("Highest marks is obtained by " + student2)
new_function(student1 = "John", student2 = "Kavish")

After writing the above code (python keyword arguments), Ones you will call “new_function()” then the output will appear as a “ Highest marks is obtained by Kavish “. Here, the keyword argument is passed to the function and the order of the argument does not matter.

You can refer to the below screenshot python keyword arguments.

Python keyword arguments
Python keyword arguments

Default parameter value in Python functions

In python, if a function is called without an argument then the arguments gets its default value is called default parameter.

Example:

def new_function(fruits = "apple"):
print("My favorite is " + fruits)
new_function("orange")
new_function()

After writing the above code (default parameter value in python), Ones you will call “new_function()” then the output will appear as a “My favorite is orange My favorite is apple“. Here, the default argument is passed automatically and it appears in the output of the function call.

You can refer to the below screenshot on Python default function parameter value.

Default parameter value in python
python default function parameter value

Python pass list as arguments to function

In python it is possible to send any data types of arguments to a function and it treat them as a same data type inside the function.

Example:

def new_function(flower):
for a in flower:
print(a)
flowers = ["rose", "lily", "tulip"]
new_function(flowers)

After writing the above code (passing a list as an argument in python), Ones you will call “new_function()” then the output will appear as a “rose lily tulip“. Here, we send the list as an argument and it will treat the same when it reaches to function.

You can refer to the below screenshot passing a list as an argument in python.

Passing a list as an argument in python
python pass list as arguments to function

Return value in Python function

We use return statement in python so that, a function return value in python.

Example:

def new_function(value)
return 10*value
print(new_function(5))

After writing the above code (return value in python), Ones you will call “new_function()” then the output will appear as a “50“. Here, the return statement follows the expression which is evaluated and it returns the value.

You can refer to the below screenshot for return value in python.

Return value in Python function
Return value in Python function

You may like the following Python tutorials:

In this Python tutorial, we learned about python functions and also how to use it like:

  • What is a function in Python
  • Creating a function in python
  • Calling a function in python
  • Arguments in python
  • Python arbitrary arguments
  • Python keyword arguments
  • Default parameter value
  • Passing a list as an argument in python
  • The return value in python

Leave a Comment