Ch3 Functions (C.S clw)
Ch3 Functions (C.S clw)
12.0
125
|230
2) Functins n Mudule:
6) Mah Motulas
) muth. (efh-67.8)
-67
Q) Floo.()
’ Retns the closes ) nteueer hal
iS Cesj than or
- 4G
>) meth. Floor (l00-22)
160
(3)po (g)
Ex »)nmalh-pue (S.R)
5) sert(x)
-’ Re turns Ihe se uare raot of
lhis unction Cac cepts
reater Hhan 2eO othe ruise
huntlme
3.1415 4a65358A343
math.
e e i s Ca methematicce
Constaht repres enting
Eulers namber approxiaukely
a.718) T+ is co mony
Cxponenhal Gned
logaritmie caleelationf.
S>math.e
2.7)8ayl82s59045
B) Rundog Moclules
’ To Dick randomhumber,wwe
ranlom molles
)randow) (2
’1+ re turnj a randomloet
hmber of
0,28(4141343
2) randiht Ca, b)
’T4 rcturnS a nteyer X be tueen
a and b ,
c) Statístics Module:
) mean ()
I4 re turms anth metic
anthmetic mean
Fx:>)
Ex sta tis is,ngun ,.3),4SJ
Wo 27.5
) mediun() Of
Ouiputi
nter rudius: 10
Area = BAH 3J4.l6
’ The keyord
a functiun
’AEtcr the keyworl,cuYnes
hentiien
an
name the
paranthesise&
eist parme tersahd the by
Colon
Chdsup the eine
NextEolus
are he par t of unctisn
We can provicle
can_pro he parametes insile
the paranthesis hich t's opti oncl
Uhderstaninq Functiuns
clef cale(): Funcion clebintin
Parame tevj
return r
a= nt (hput Entcr a no, ))
pont (cal a))
Fanction Coll
Funcisn Boy
’Funeion healer
Defining Functionsi
AFunctisn ha tuomainparts
Functisn heuder
Funcon
’ Functin header
S= g Funetion body
Yeturn
Fwnction Healer:
Jhc first Linc of Anetion definition
beuinsaith clee Cahd cndinith
Colon
Paramctersz
Vanab les hat are listcl urthin
he p ranthesis o a Function hcacler
FahctiSn Bodt
-The blocks of Statementr in deted
Statements benea th fune HRon header
hd ketincs he c ctlon yerformel
he
by he functiun
Indene ion
· The blunk Space in thc
of a Statemcnt uilhn beginaing
A Statcmen ( within Scme blocls
have SCame hclen tation
Jn fanction Cu
calc (5)
Calcla).
By default, py thon
py thn names t e
Scament uith top lavd statements
asmain
def function] :
prodran
ald
functlon
hunabers thugh c
def caleSum Coc,y)
return S
hum) floallinput C Entey Lst no.)
5 num2 floutibhut l" Entor 2nd ho. ;
6 Sum calcsum (numl, num2)
7 phht
) FOo oh execuioni
Fx:
clef check (ab, c):
ChecsCXiy,2)
Checs (2,24.g)
checs (2,5,7)
prvtded
he arqueng ent must b
The
for al parascters
2) Default Arquementrz
Py thon cllows
Ossian dekault valner to a
functto 's parame ter( . h i c h
usefud ih Cue matching Cunnement
pasel inFunctisn call
Statement., he dlefaut vaics Ave
Speuifie & in the fnctibnheaclers o
functisn cletinition
1 Non default Orquements Cahno
follow default crg
Cirg uements
Ex!
det
rcitP,t R OD):
ST = in eest (l0o0,2)
parame teik
¥ HDarament
dealt value ih having header
Fuhction
a
guement in
any
Orlen phvided J u ile the
name arguennentt.
Ex:
intercst+P=20 o0,¬- 2 n te = O.lo)
ntercst (t=2 rate = Oul2, P-dooo)
Exerise:
Chunae (a bic,d):
m mat (a,bii)
nmin (a,by cocl
t a intupe wernnt
mbine hutiphe
ara Call
e ACtytpesarguementtoyou alwSallows pytnn
Mul i ple Vsing
34 38
34 30
30aO
Ouiputi
(2g) prht
abcly) X
(,y print
priht,y)l) abe
abC(,y)
atb rctun
bi4)i (ara abc def
Output
ghyi) prht
ntur he
def interes( P, t2, rate o.09):
re turn Pt rate
jntenes + (5000, t 5)
SI
retumG
returna*3
Ex:
addes ult = Suum la,b).
prink C Sum (3,4) )
def qreet(namne):
print ("tello,nume)
returo
3) Vid functionuith
print (t)
Output
(9,9, 16)
Ex:
def seucrecl (xy. z):
return t , yxy, 2* z)
print (The tcturn e values are 3
print V), Va, v3)
Cormps ition i
> Compositioh in qenerLl re ers to ustn
an
expression as past part
Larger expression
as a part
, Or a stoatemet
o arer Statement.
Ex :
2. tess ( a or b)
3. int ( str (52)
4. ini ( FLoa4 C"5).5")2
lape of Scope ?
DLocal Scope
Jhe vaDubles declarcd insiAa
the Fun chion clekinition are colled
Local variables.
B=30
poht (A:A
prnt "S:B)
Punct )
Funct,B)
Ouciput
B:30
B outsile of funt 30
EX:
der unc C):
A50
A)
priht (Tnsile Fnction :"
A
Prh+ C"outside hunetion