In this session we will be working with functions in Python. Function is a piece of code that performs a specific task and we can re-use that function any number of times in our code. If we feel that the functionality that we are coding is too long, then its better to decompose the code into multiple parts/smaller functionalities and create functions are around them.
For example, if we are building code to create calculator, then as a developer we can think of creating following functions – addition, multiplication, division and subtraction.
Types of functions in Python:
|Build-In Function||Module||User Defined|
|Comes with python ex. print()||Pre installed that needs to be imported before using ex. math, sin||Written by users for specific tasks|
Syntax of Function in Python:
- Function definition should always start with keyword def
- Then we have to mention function name followed by parenthesis and colon
- In the above simple example, parenthesis is empty because we have not used any parameters
- Next comes the body of the function or the instructions list which will be executed when function is called. We have to follow indentation rules for writing function body like we saw in loops
- When we have to call the function, we just have to write the name of the function in the code: fun_name()
Examples of Functions in Python:
1. here we will create a function to print a simple message
def message(): print("I am in function") print("First Function") message()
2. once the function execution is completed, the control goes back to the line after function invocation
def message(): print("I am in function") print("First Function") message() print("Function Completed")
3. We should not call a function before it is defined. Function definition should come first than function call
print("First Function") message() print("Function Completed") def message(): print("I am in function") This code will throw error because we have called the function before defining it
4. We should not use same name for function and any variable. If we do so, the previous role of the function is lost
def fun(): print("Inside function") print("call function") fun() fun=1 fun()
In this example, we have defined a function. Then we have called the function. After calling, we initialized fun=1, this means that now the function does not exist anymore and fun is now a variable. Hence print statement and first call to function will work file but second call to fun() will throw an error.
5. In above example, instead of making a second call to function, we should use fun an a normal variable like below:
def fun(): print("Inside function") print("call function") fun() fun=1 print(fun)
However, it’s not a good practice to put same name for function and any variable. We should use different names.
6. Defining multiple function. We just have to remember that function is defined before it is called
def fun1(): print("Inside function 1") print("call function 1") fun1() def fun2(): print("Inside function 2") print("call function 2") fun2()
Previous Session: https://mytechdevice.com/python-list-comprehension/