What are Python Functions?
In the python programming context, function is a named collection of python statements combined together to perform specific task. Python def keyword is used to define the function in python. It is a package of generic python code which you can be used whenever required in your code.
There are many built in functions available in the python. Also, there are possibility to create new customised, user written functions in python using python def keyword.
Python built-in functions are already predefined into python standard library hence you don’t need to import any modules to use built-in functions.
You have already seen many python functions since my first article –What is python? Starting from basic program to print “Hello World” to more complicated and lengthy codes. You might have written “print” statement thousands of time, but did you ever wonder how print – statement works?
Similarly, there are other functions we use regularly such as – input, eval, int, float, range, and type. These are the common built-in functions included in the python standard library hence can be available at any time.
Apart from this we do have many other built in functions in python but to use those functions you need to import related modules in the beginning of your code.
Use defined function
The function we define /creates by ourselves to perform any specific task known as user defined function. Python def keyword is used to define user defined functions.
The whole concept of functions revolves around two things – first, how you define / write your function and second, how and where you call that function by supplying necessary parameters to the function.
#define your python function (python def Keyword) def FunctionName(parameters) . . . . . . . . . . . . . . . . . . #call python function def FunctionName(parameters)
Try to understand simple python function – show() to display some predefined text message whenever this function gets called in the code.
#define show() function def show(): print("Show function executed") #call show() function show()
Show function executed >>>
Observe this example – show() is your user defined/created function but inside show() function we are calling built-in print() function.
Now we know when we call show function, it will execute lines of code written inside the function. In a similar way, when we are calling print() function – some sort of python code executes which is available in the python standard library.
There are many generic codes already written in python and imported into python standard library. In function irrespective of its types- either built-in or user defined, there are some rules needs to be followed as per logic written inside that function in ordered to reuse the code.
Python Function to add two numbers
This example demonstrates simple python function to add two numbers by taking inputs from user.
#definefun function - add() def add(a,b): result=a+b print("Addition of two numbers :", result) #take input from user a=int(input("Enter First Number :")) b=int(input("Enter Second Number :")) #call add() function to perform addition. add(a,b)
Show function executed Enter First Number :25 Enter Second Number :100 Addition of two numbers : 125 >>>