Functions in Python

In this tutorial, we would learn about Functions in Python. A function is a block of code which allow us to reuse code. Python provides us rich collection of in-built functions like print(), mkdir(), rmdir() etc. which we can use by just calling them. In Python, we can create our own functions. This kind of functions known as user-defined functions. Here we will learn How to create a function, how to call it. return statement etc.

Defining a function in Python

Defining a function in Python is so easy. we use def keyword in Python to define a function. Check this example to understand it.

def my_function():
    print ("Hello This is my_function")
    return

Here you can see we used def keyword to define a function then we wrote function name followed by (). After we wrote: symbol to state body of the function. At the end we wrote a return statement which is optional we will write a return statement without parameter this is equal to return none.

Calling a function in Python

After defining a function we can call our function any number of time in our code. Check this example.

def my_function():
    print ("Hello This is my_function")
    return

#calling our function Here 

my_function()

 

Spread the love
Scroll to Top
×