function

By | May 4, 2018

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:

Syntax:

  1. def keyword followed by function name followed by : .
  2. Block of code to be executed in the function.
  3. While calling function just give function name.

After defining function one has to call to execute it.

#function example.
def funcex():
             print(“Hi, I am a function”)

funcex() #calling function

Output:

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.

#function example.
def funcex(name):
          print(“Welcome “+name)

funcex(“Shifu”)                        #calling function
funcex(“Master”)                    #calling function

Output:

Welcome Shifu
Welcome Master

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.

#function example.
def funcex(a,b):
           return a+b
print(“Sum is “,funcex(3, 5))             #calling function
print(“Sum is “,funcex(-3, -5))          #calling function

Output:

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:

  1. import keyword.
  2. followed by module keyword.
  3. 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:

  1. from keyword
  2. followed by module name
  3. import keyword
  4. 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:

#import example
import random
#Here randtint(10,20) function will give
#any random number any number from 10 till 20(include 20)
r=random.randint(10,20)
print(“Random number generated is”,r)

output:

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

def division(a,b):

        try:

              return a/b

        except ZeroDivisionError:

               print(“b cannot be zero, Please pass another value”)

print(division(5, 0))

Execute this and comment us the output.

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.