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.
Page Contents
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()

Parvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​