INTRODUCTION
Function is a group of statements that
  exists within a program for the
  purpose of performing a specific
  task.
ADVANTAGES:
 Program handling becomes easier.
 Code reusability
 Compact Code
 Easy updation
TYPES OF FUNCTIONS
Built in functions:
 Predefined functions that are already
  available in Python.
 No need to import any module.
 Example:len(),str(),print()
Modules:
 the set of functions stored in a file is called
  file.
 if u want to use any function need to import
  the module.
 Example:math-pow(),sin()
USER DEFINED FUNCTIONS
  A function is a set of statements that
   performs a specific task;
 A common structuring element that allow
   to use a piece of code repeatedly in
   different parts of a program.
SYNTAX:
def fun_name(comma separated args):
     <function statements>/function –body
statements
USER DEFINED FUNCTIONS
Components of user defined function:
 keyword def marks the start of the function header.
 A function name to uniquely identify the function.
 Parameters(arguments) through which we pass
  values to a function(optional).
 A colon(:) to mark the end of the function header.
 One or more valid Python statements that makes up
  the function body. Statements must have the same
  indentation level.
 An optional return statement to return a value from
  the function.
 Function must be called /invoked to execute its code.
Example:
i)def func1(a,b):
   c=a+b
   print(c)
func1(2,3)
ii)def func2(a,b):
    c=a+b
    return c
x=func2(2,3)
print(x)
RETURN STATEMENT
  return command in function is used to end the
   function of the function call and also specifies
   what value is to be returned back to the calling
   function.
Example:
def fun3(r,w,t):
  r=w+t
  return r
  w=r-t
  print(w)
print(fun3(10,30,45))
print(“hi”)
simple program...
#WRITE A FUNCTION TO FIND THE
   FACTORIAL OF A GIVEN NUMBER
def fact(num):
   result=1
   while num>=1:
       result=result * num
       num=num-1
   return result
for i in range(1,5):
   print("the fact of" ,i,fact(i))
output:
the fact of 1 1
the fact of 2 2
the fact of 3 6
the fact of 4 24
Function returning multiple
values
A return statement in Python function can
   return any number of values and the
   calling function should receive the value
   in any one of the following way.
i)By specifying the same number of
   variables on the L.H.S of the assignment
   operator in a function call
Example..
def sum_sub(a,b):
   sum=a+b
   sub=a-b
   return sum,sub
sm,sb=sum_sub(10,20)
print(sm)
print(sb)
o/p:
30
-10
Example...
def sum_sub(a,b):
   sum=a+b
   sub=a-b
   return sum,sub
s=sum_sub(10,20)
print(s)
o/p:
(30,-10)
Example
def func():
   pass
a=func()
print(a)
PARAMETERS AND ARGUMENTS
IN FUNCTIONS
Parameters are defined by names provided in the
   parentheses when write the function definition
def funs(a1,a2):#parameters or formal parameter
    a3=a1**a2
    return a3
s1=int(input(“enter a number”))
s2=int(input(“enter a number”))
print(funs(s1,s2)) #arguments or actual parameters
Identify which of the following are
invalid
i)def product(a,b):
     print(a*b)
ii)def product(a+1,b):
     print(a*b)
iii)def product(5,’b’):
     print(a*b)
Types of user defined functions
i)Function with no arguments and no
   return values:
def welcome():
     print(“hi”)
     print(“hello”)
welcome()
ii)Function with arguments but no return
   value
def cube(num):
    c=num*num*num
     print(c)
cube(3)
iii)Function with no argument but return
    value:
def cube():
    c=3*3*3
    return c
a=cube()
print(a)
iv)Function with argument and return
   value:
def cube(num):
    c=num*num*num
     return c
a=3
print(cube(a))
    Types of arguments
Positional arguments:
  Positional arguments are arguments
  passed to actual arguments of a function
  call in a correct positional order.
def power(a,b):
    c=a**b
    return c
power(10,2)
Default arguments:
   A default argument is an argument that
  can assign a default value ,if a value is
  not provided in the function call for that
  argument.
Example:
Default arguments must not followed by non
  default arguments
def interest(principal,rate,time=15):
def interest(principla,rate=8.5,time=15):
def interest(principal,rate=8.5,time):#invalid
not passing any to the named
   argument,then only default value will be
   taken.
def interest(principal,rate=0.5,time=2):
     si=principal*rate*time
interest(2000)
interest(1000,0.4)
interest(200,0.3,3)
Keyword(Name) arguments
Keyword arguments are the named
  arguments with assigned values being
  passed in the function call statement.
Example:
def greet_msg(name,msg):
  print(“hello”,name,msg)
greet_msg(name=“Vinay”,msg=“GM”)
greet_msg(msg=“GM”,name=“Ram”)
greet_msg(name=“Sam”,”GM”)
In a function header,any parameter cannot
   have a default value unless all parameters
   appearing on its right have their default
   values.
INVALID AND VALID FUNCTION HEADER
   WITH DEFAULT VALUES:
def intr(pri,ti,ra=0.05): #valid
def intr(pri,ti=8,ra=0.09):#valid
def intr(pri=2000,ti=3,ra=0.10): #valid
def intr(pri=4000,ti,ra): #invalid
def intr(pri,ti=9,ra):#invalid
PASSING MUTABLE AND
IMMUTABLE OBJECTS TO
FUNCTIONS
 PASSING LISTS TO FUNCTIONS:
def my_function(food):
   food.append(30)
   print(food)
fruits=[10,20,30,40,50]
my_function(fruits)
print(fruits)
o/p:
10,20,30,40,50,30
10,20,30,40,50,30
PASSING STRINGS TO THE
FUNCTION
String can be pas to a function as an passed to an
    argument.Since it is an immutable object in Python.
Thus a function can access a value to string but cannot
    modify it.
Example:
def my_function(food):
   food+="cherry"
   print(food)
fruits="appleandbanana"
my_function(fruits)
print(fruits)
o/p:
appleandbananacherry
appleandbanana
def my_function(food):
   f1=food+[6]
   print(f1)
fruits=[10,20,30,40,50]
my_function(fruits)
print(fruits)
o/p:
[10, 20, 30, 40, 50, 6]
[10, 20, 30, 40, 50]
SCOPE OF VARIABLES
All variables in a program may not be
   accessible at all locations in that
   program
Scope of variables refers to the part of the
   program where it is visible ie., area
   where we can refer it.
TYPES OF SCOPE
GLOBAL:
Can be accessed inside or outside of the
  function.
LOCAL:
Cannot be accessed outside the function
SCOPE OF VARIABLES
Part(s) of a program within which a name
  is legal and accessible is called scope of
  the name.A particular piece of code or
  data item would be known and can be
  accessed therein.
Global variable:
  A global variable is a variable defined in
  the “main”program.Such variable are
  said to have global scope.
Example:
def sum(a,b): # a and b are local variables
   sum=a+b
   return sum
x=10       #global variable
y=45       #global variable
print(sum(x,y))
Local variables:
  A local variable is a variable defined with
  a variable definition within a
  function.Such variables are said to have
  local scope
Mutability and Immutability
Changes in immutable data types are not reflected in
    the caller function at all
def sum(a,b):
   a=a+1
   print(a)
   return
x=10
sum(x,5)
print(x)
o/p:
11
10
  Changes ,if any, in mutable data types are
   reflected in caller function if its name is not
   assigned a different variable or a datatype.
Example:
def s3(li):
   li.append(3)
   print("inside function",li)
a=[10,20,30]
s3(a)
print("outside function",a)
o/p:
inside function [10, 20, 30, 3]
outside function [10, 20, 30, 3]
   Changes in mutable types are not reflected in the
    caller function if it is assigned a different variable or a
    different data type
Example:
def s3(li):
   new=[9,10]
   li=new
   li.append(3)
   print("inside function",li)
a=[10,20,30]
s3(a)
print("outside function",a)
o/p:
inside function [9, 10, 3]
outside function [10, 20, 30]
The time for which a variable or name remain in
   memory is called Lifetime of variable
Variable in global scope but not in local
   scope:
def calcsum(x,y):
   s=x+y
   print(n1)
   return s
n1=10
n2=20
print(calcsum(n1,n2))
Variable neither in local scope nor in
  global scope:
def greet():
  print(name)
greet()
Same Variable name in local scope as well
   as in global scope
def state1():
   tigers=15
   print(tigers)
tigers=95
print(tigers)
state1()
print(tigers)
O/P:
95
15
95
def state1():
   global tigers
   tigers=15
   print(tigers)
tigers=95
print(tigers)
state1()
print(tigers)
o/p:
  95
  15
  15
RECORD PROGRAM 5
Write an interactive menu driven program in python to accept the details of
employees such as employee number, name, Basic ,HRA , DA from user and
calculate annual salary of every employee with deductions like PF and to
calculate the net salary(Basic+HRA +DA –PF)using dictionaries
def salary(n):
  d={}
  pf=int(input("enter the pf amt"))
  for i in range(n):
     eno=int(input("enter the employee number"))
     ename=input("enter employee name")
     basic=int(input("enter the basic value"))
     hra=int(input("enter the hra value"))
     da=int(input("enter the da value"))
     sum=basic+hra+da
     p=sum*pf/100
     d[eno]={"netsalary":sum-p,"annualsalary":sum-
    p*12,"nameofemployee":ename,"basics":basic,"Houserentallow
    ance":hra,"Dallowance":da}
  return d
m=int(input("enter the no.of employees"))
d=salary(m)
print(d)
s=int(input("enter the eno to search"))
for i in d:
   if i==s:
       print(d[i])
       break