FUNCTIONS IN PYTHON
In this tutorial, we are going to learn about functions in python, how to define them, call them as well as how to set different parameters to them.
What are Functions in Python?
A function is block of structured code that can be reused over and over again inside a python program to perform a particular task. You can also build user-defined functions and use them to compose Python module programs.
Python enables us to split a big program into the chunks of function-friendly building blocks. A function can be called several times to provide the python program with reusability and modularity.
How to define a Function?
In order to define a function all you need to do is follow a few guidelines:
- First, to define a python function you will use the keyword ‘def’ before the describing the name of the function. ‘def’ stands for ‘define’. Once you have added def followed by the function name (which should follow the same rules that we used while defining the variables in python), and after defining the function name add parentheses as well with colon in the end. Syntax of writing a function: def function_name():
- Next, you will add the function body, this part decides what your function is going to do. Add valid python code here.
- You can add a return statement if you want. However, that is completely optional.
A normal function would look like this:
def function_name(parameter): function_body return
How to Call a Function?
After defining, naming and specifying the parameters and arranging the structure of code blocks then it’s time to call the function. You can easily call the function directly inside the python program. For example:
def name_printing(): print("My name is Python Tricks") name_printing()
Output will be:
My name is Python Tricks
Adding Parameters in Function Calling
We can add parameters inside the parentheses of the function, we can add number of parameters to be separated by comma. If you change the value of the parameter, then this will affect during the function calling as well. For Example:
def change_name(name, age): print ("My name is " + name + " and my age is " + str(age)) change_name("Hira", 25) change_name("Smith", 30)
Output will be:
My name is Hira and my age is 25 My name is Smith and my age is 30
Setting a Default Parameter
A default argument (parameter) stands for a value that is given inside the function definition. For example, you don’t want to define a during function calling then you may want to set a default argument in the function definition, like:
def change_name(name, age=25): print ("My name is " + name + " and my age is " + str(age)) change_name("Hira")
My name is Hira and my age is 25
*args or Multiple Arguments
Sometimes while writing a function, you may notice that there would be more arguments required than what has been decided earlier in the function definition. To tackle this issue you just need to enter the name of a parameter with an asterisk (*) in the beginning.
def my_colors( *colors ): print ("Output is: ") for color in colors: print (color) my_colors('red','green','yellow')
Output is as follows:
Output is: red green yellow
**kwargs or Multiple Keyword Arguments
The **kwargs in python function definition is used to pass keyword, variable-length argument list. You can think of this as a dictionary with key-value pairs. It is represented with double asterisk (**) For Example:
def person_info(**my_info): print ("Output is: ") for key, value in my_info.items(): print (key, value) person_info(name="hira", age=25)
Output will be:
Output is: name hira age 25
You can add as many key-value pairs as you want.