Contents

## ### Function:-

function is group of program statement grouped together as a unit and this unit of code is executed only when user calls this function can be categorized as :-

#### 1.user defined function:-

this type of functions are created by users as per programming requirement.

#### 2. in build function(library function):-

this type of functions are readily available in python and user need only to call this function.

1.the function reduces length of program.
2.improve re usability of code.
3.easy to understand.
4.improve efficiency and execution time of program.
example of function

#### Q 1 write program using function the radius as a parameter and print a area and perimeter of circle.

print(“area=”,a)
print(“perimeter=”,p)
circle(r)

#### Q 2. program that receives an integer and function prints its table.

ans. def table(n):
for x in range(1,11):
print(n,”x”,”=”,n*x)
print(“**********”)
r=int(input(“enter a number”))
table(r)

#### Q 3. program to calculate simple interest.

ans. def SI(principle,time,rate):
a = P*T*R/100
print(“simple interest=”,a)
P = int(input(“enter principle”))
T = int(input(“enter time”))
R = int(input(“enter rate”))
SI(P,T,R)

### default parameter:-

default parameter is kind of parameter whose default value is given at the time of function definition.
if user passes parameter to function than actual value taken by the parameter in the function.
if user does not passes value to function than default values would be considered by the function.

### Factorial:-

def factorial(n):
p=1
for x in range(1,n+1):
p=p*x
return p
k=int(input(“enter a number”)
print(“factorial of”,k,”=”,factorial(k))

### variable length parameter:-

in variable length parameter n no. of values parameter can be used. In a function declare a variable length parameter a symbol “*” is used.

### variable scope:-

1. local:-local variable declared inside the function globe.
scope of local variable is within the function only.

2. global:-declared outside the function.
scope of global variable is through out the program including all the function.

### key word argument(parameter):-

def printing(name,age):
print(“name=”,name)
print(“age=”,age)
printing(“rahul=”,15)
printing(12,”ajay”)
output
name=rahul
age=15
name=12
age=ajay

### Anonymous function:-

can be defined and used in python using keyword lambda in this type of function one or more parameter can be passed but they must have only one expression.

#### Q 4.write a program using anonymous function for the following.

1.cube value of a number.
print(“cube value of 5=”,lambda 5: 5*5*5)
output
cube value of 5 = 125

2. multiplication of two no.
x=5
y=20
z=lambda x,y : x*y
print(z)
output
100

### random module

#### random:-

this generates floating values between 0 and 1. does not require any argument.
example:-
>>>import random
>>>random.random()
output
0.9541645145

working with functions in python important questions