8/14/2019 Python - Its Functuns and Syntax
1/24
FUNCTIONSITS TIME TO BE ORGANISED
8/14/2019 Python - Its Functuns and Syntax
2/24
TO THE
RESCUEPOINT
As programs grow, the code often becomesmore compe!" How to cope #p $
8/14/2019 Python - Its Functuns and Syntax
3/24
Dont duplicate your data
instead REUSE it using FUNTIONS
8/14/2019 Python - Its Functuns and Syntax
4/24
A na!ed se"uence o# state!entst$at per#or!s so!e use#ul
operation%
%hat &s a f#nct&on $
8/14/2019 Python - Its Functuns and Syntax
5/24
For e%g% &>>> type(32)
< type int >
'ere(Function na!e& type
Argu!ent passed& )*
8/14/2019 Python - Its Functuns and Syntax
6/24
For e%g% &
val=50
>>> type(val)
< type int >
'ere(
Function name& type
Argument passed& +al ,parameter-
Anot$er e.a!ple
8/14/2019 Python - Its Functuns and Syntax
7/24
/yt$on pro+ides 0uilt1in #unctions t$atcon+ert +alues #ro! one type to anot$er%
For e%g% &
>>> int(3.)3 !!output
T'pe con(ers&ons
#loat int
8/14/2019 Python - Its Functuns and Syntax
8/24
T$e int #unction ta2es any +alue and converts
it to an integer( i# it can( or complainsot$er3ise&
For e%g% &
>>> int("#ello")
$alue%rror& invalid literal 'or int()& #ello
8/14/2019 Python - Its Functuns and Syntax
9/24
T$ere are * types o# #unctions &
4% In0uilt types
*% User1de#ined
T)PES OF FUNCTIONS
8/14/2019 Python - Its Functuns and Syntax
10/24
/yt$on $as modulest$at pro+ide !ost o#t$e #a!iliar #unctions%
A moduleis a 5le t$at contains a collectiono# related #unctions%
For e%g% &
Mat$ !odule
IN*UI+T FUNCTIONS
8/14/2019 Python - Its Functuns and Syntax
11/24
speci#y t$e na!e o# t$e !odule and t$ena!e o# t$e #unction( separated 0y a dot%
T$is #or!at is called dot notation%
For e%g% &
>>> radians = 0.
>>> eigt = mat.sin(radians)
How to access f#nct&ons from
mod#es$
8/14/2019 Python - Its Functuns and Syntax
12/24
Functions de#ined 0y t$e user as per $isre"uire!ents are called user de#ined#unctions%
User def&ned F#nct&ons
8/14/2019 Python - Its Functuns and Syntax
13/24
def twice(number):
Finds the twice of the number .
int answer
answer=2*number
return answer
ef&n&ng a f#nct&on
8/14/2019 Python - Its Functuns and Syntax
14/24
>>>def twice(number):
Finds the twice of the number .
int answer answer=2*number
return answer
Aternat&(e
8/14/2019 Python - Its Functuns and Syntax
15/24
efde#ines a #unction numbersis a para!eter & a +aria0le used to
$old an argu!ent T$is doc string tells 3$at t$e #unction does A #unction t$at co!putes a +alue !ust returnit
twice(101) 3ill return *6*%
>>> ef twice(number): Fin s the twice of the number . int answer
answer=2*number return answer
8/14/2019 Python - Its Functuns and Syntax
16/24
So!e o# t$e #unctions yield results 3$ileso!e not%
FRUITFU+ and -OI FUNCTIONS
8/14/2019 Python - Its Functuns and Syntax
17/24
For e%g% &
777 type,)*-
8 type 9 int 9 7
*is is a void 'unction as it not return anyvalue.
8/14/2019 Python - Its Functuns and Syntax
18/24
For e.. :def twice(number):
Finds the twice of the number int answer
answer=2*numberreturn answer
!his function returns a "a#ue bac$ to the
main. %o this a fruitfu# function.
8/14/2019 Python - Its Functuns and Syntax
19/24
A #unction call is li2e a detour in t$e :o3 o#e.ecution% Instead o# going to t$e ne.t
state1 !ent( t$e :o3 ;u!ps to t$e 0ody o#t$e #unction( e.ecutes all t$e state!entst$ere( and t$en co!es 0ac2 to pic2 up3$ere it le#t o##%
E!ec#t&on of f#nct&ons
8/14/2019 Python - Its Functuns and Syntax
20/24
< =e can na!e a group o# state!ents( 3$ic$!a2es our progra! easier to read andde+ug% Functions can !a2e aprogram smaller 0y
eli!inating repetiti+e code% >ater( i# you!a2e a c$ange( you only $a+e to !a2e itin one place%
=ell1designed #unctions are o#ten use#ul#or !any progra!s% Once you 3rite andde0ug one( you can reuseit%
%h' f#nct&ons$
8/14/2019 Python - Its Functuns and Syntax
21/24
=$en a #unction calls anot$er #unction%
Nested F#nct&ons
8/14/2019 Python - Its Functuns and Syntax
22/24
For e.. :
def twice(number):
Finds the twice of the number
int answeranswer=2*numberreturn answer
def twice&s'uare(number):
Finds the s'uare of twice of the numberint t
int answer
t=twice(number) answer=t*t
return answer777t3ice?s"uare,*6-4@66
8/14/2019 Python - Its Functuns and Syntax
23/24
RECURSION
*e a+ility o' a 'unction to callitsel' is called ,%-,/1.
8/14/2019 Python - Its Functuns and Syntax
24/24
)
*4
Blasto##
For e"g" .def co#ntdown/n0.
&f n 12 3.pr&nt 4*astoff54
ese.pr&nt n co#ntdown/n670