What is function:
Function is block of code that gets executed multiple times.
Why function is required:
Without function one has to write the same piece of code again and again. Without functions you have to Copy and paste every time.
How to create and Use function:
- def keyword followed by function name followed by : .
- Block of code to be executed in the function.
- While calling function just give function name.
After defining function one has to call to execute it.
print(“Hi, I am a function”)
funcex() #calling function
Hi, I am a function
You can call a defined function any number of times. One has to cultivate habit of developing functions because duplication of code is bad practice.
defining function with parameters:
Most of the times there is necessity to pass parameter during run time. To do this place your parameter inside () while defining and calling functions.
funcex(“Shifu”) #calling function
funcex(“Master”) #calling function
By default parameter is always of string data type. Just recall built in functions so far we user. Eg: print()
Functions not only executes block of code but also returns some value. Just recall built in functions of this sort so far we user. Eg:len() which will return length of string on which it is being used.
print(“Sum is “,funcex(3, 5)) #calling function
print(“Sum is “,funcex(-3, -5)) #calling function
Sum is 8
Sum is -8
There is a function in Python which will generate random numbers. Point is how to use these builtin functions. These functions and statements will be grouped into certain modules. One can use these modules by importing those in respective program.
Not only builtin modules we can also import user defined functions in one module to another module. So the syntax to import is:
- import keyword.
- followed by module keyword.
- in case of multiple modules separate those by comma.
I am considering a case where module contains 50+ functions but I need only one of those. Do I still need to go for import of entire module. Well, there is provision to import the function which you need and Syntax is:
- from keyword
- followed by module name
- import keyword
- followed by function name
In case to import all functions then the syntax is import *
Whenever a user imports a module, it will look in builtin modules first, after that it will look in user defines. Let us see example:
Code snippet is:
#Here randtint(10,20) function will give
#any random number any number from 10 till 20(include 20)
print(“Random number generated is”,r)
Random number generated is 16
We have learnt how to define and use functions. So what if our function met any accident in run time. We should start with precautions while defining it when it is prone to run time errors. I will take a function which will return result of division of two numbers(i/j). What if j=0. Its mathematically infinity. We have to handle this case as well to have a good practice of programmer. The provision Python3 providing is exception handling.
See interpreter is scolding us for above program. Lets handle now:
Write code to execute in try block. Whenever exception comes catch it in except.(Just like try catch in Java, For someone who knew Java).
#while loop continue Example
print(“b cannot be zero, Please pass another value”)
Execute this and comment us the output.