Tuesday, 24 January 2017

FUNCTIONS IN PYTHON

Functions is a syntax or structure in which we represent the reusable business logic. Functions were not executed automatically. Functions will be executed whenever we makes a function call. We can call one function for n number of times. Python supports two types of functions. They are –

1.  Built-in functions – The functions which comes along with python software are known as pre-defined or built-in functions. Example – id(), input(), raw_input(), type(), len() etc.

2. User-defined functions – The functions which are developed by the programmers explicitly according to their business requirements are called as user-defined functions. We can define the user-defined functions by using following syntax –
Def function_name(parameters):
“”” docstring “””
--------------
--------------
--------------
return value (optional)

Note – Within the function syntax, parameters and return statements are optional.

Example –
print 'hello'def f1():
    print 'welcome'f1()
f1()
print 'bye'
Output –
hello
welcome
welcome
bye


Parameters – Parameters indicates input of the function i.e. if any function contains parameters at the time of calling that function then we have to pass values to those parameters, otherwise we will get error

Example –
def greet(name):
    """This function greets to the person passed
    in the parameter"""
    print 'hello',name,'good morning!'greet('rakesh')
greet('hacker')

Output –
hello rakesh good morning!
hello hacker good morning!

Note – We can define n number of parameters to a function.

Example –
def add(a,b):
    print 'Sum of ',a,'and ',b,' is : ',a+b
add(100,200)
add(12,456)

Output –
Sum of  100 and  200  is :  300
Sum of  12 and  456  is :  468


Return Statement – Return statement indicates what data is going to be given after execution of that function is over. If the function doesn’t contains return statement then by default it returns none. If function contains return statement then we can use the return value by storing that value into other variable.

Example –
def add(a,b):
    return a+b
x=add(100,200)
print x

Output –
300


Example –
def m1():
    print 'hi'
print m1()

Output –
hi
None


Types of arguments – In python language for functions we can pass four types of arguments. They are –

1. Required arguments –

def f1(a,b):
    print a+b
f1(10,20)
 
 
2. Default arguments –
 
Example –
def f1(a="good morning"):
    print 'hello rakesh ',a
f1()
f1('good evening')
 
Output –
hello rakesh  good morning
hello rakesh  good evening
 
Example –
def f1(a,b='good morning'):
    print 'hello ',a,b
f1('rakesh')
f1('barsha')
f1('bikash','good evening')
 
Output –
hello  rakesh good morning
hello  barsha good morning
hello  bikash good evening
 
Note – After default arguments we are not allowed to declare non-default 
arguments.
 
Example –
def f1(a='rakesh',b):
    print 'hello',a,b
f1('bikash','good evening')
 
Output –
  File "C:/Users/om/PycharmProjects/Pythonn/if3.py", line 1
    def f1(a='rakesh',b):
SyntaxError: non-default argument follows default argument
 
 
3. Keyword arguments –
 
Example –
def f1(name,msg):
    print 'hello',name,msg
f1(msg='good evening',name='rakesh')
f1(name='barsha',msg='good morning')
f1('bikash','good morning')
f1('good morning','apurba')
 
Output –
hello rakesh good evening
hello barsha good morning
hello bikash good morning
hello good morning apurba
 
Example –
def f1(name,msg):
    print 'hello',name,msg
f1('rakesh',msg='good morning')
f1(msg='good morning','rakesh')
 
Output –
  File "C:/Users/om/PycharmProjects/Pythonn/if3.py", line 4
    f1(msg='good morning','rakesh')
SyntaxError: non-keyword arg after keyword arg
 
Note – Non keyword argument should not be preceded by keyword arguments.
 
 
4. Arbitrary arguments – We can pass n number of values.
 
Example –
def greet(*names):
    print names
    for p in names:
        print p
greet('rakesh','barsha','apurba','bikash')
greet(10,20,30,40,50,60)
 
Output –
('rakesh', 'barsha', 'apurba', 'bikash')
rakesh
barsha
apurba
bikash
(10, 20, 30, 40, 50, 60)
10
20
30
40
50
60

No comments:

Post a Comment