How to call a function in Python

In this tutorial, we will learn how to call a function in Python.

In Python, a function is a reusable code that does some specified task. A function performs a task only when it gets called in our program. Usually, a function has parameters where we can pass our data. These functions return results using the return statement. If we don’t use the return statement, it returns the default value i.e None.

Defining a function in Python

First of all, to call any particular user-defined function, we need to create that function. We can create or define a function in python using the def keyword followed by a function name with zero or more parameters.

Syntax:
def  function_name(parameters):
          #statements

Calling a function in Python

We can simply call any function with the function name followed by parenthesis consisting of zero or more parameters. These parameters can be integer, float and string.

Syntax:
funtion_name(parameters)

Python program to create and call a function

In the below program, we defined a function greeting with a single parameter username. We called this function with the function name greeting followed by a string parameter “Rohit” in parenthesis.

def greeting(username):
    print("Hello " + username)

greeting("Rohit")

Output:

Hello Rohit

That’s it! I hope you have understood how to call a function in python. If you have any doubts, feel free to post them below.

Also, do check our other related articles,

Leave a Reply

Your email address will not be published.