Python - Its Functuns and Syntax

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

FUNCTIONS

ITS TIME TO BE ORGANISED

As programs grow, the code often becomes more comp e!" How to cope #p $

TO THE RESCUE POINT

Dont duplicate your data instead REUSE it using FUN TIONS

%hat &s a f#nct&on $


A na!ed se"uence o# state!ents t$at per#or!s so!e use#ul operation%

For e%g% & >>> type(32) < type int > 'ere( Function na!e& type Argu!ent passed& )*

Anot$er e.a!ple
For e%g% & val=50 >>> type(val) < type int > 'ere( Function name& type Argument passed& +al ,parameter-

T'pe con(ers&ons
/yt$on pro+ides 0uilt1in #unctions t$at con+ert +alues #ro! one type to anot$er% For e%g% & >>> int(3. ) 3 !!output

#loat

int

T$e int #unction ta2es any +alue and converts it to an integer( i# it can( or complains ot$er3ise& For e%g% & >>> int("#ello") $alue%rror& invalid literal 'or int()& #ello

T)PES OF FUNCTIONS
T$ere are * types o# #unctions & 4% In0uilt types *% User1de#ined

IN*UI+T FUNCTIONS
/yt$on $as modules t$at pro+ide !ost o# t$e #a!iliar #unctions% A module is a 5le t$at contains a collection o# related #unctions% For e%g% & Mat$ !odule

How to access f#nct&ons from mod# es$

speci#y t$e na!e o# t$e !odule and t$e na!e o# t$e #unction( separated 0y a dot% T$is #or!at is called dot notation% For e%g% & >>> radians = 0.( >>> )eig)t = mat).sin(radians)

User def&ned F#nct&ons

Functions de#ined 0y t$e user as per $is re"uire!ents are called user de#ined #unctions%

,ef&n&ng a f#nct&on
def twice(number): Finds the twice of the number . int answer answer=2*number return answer

A ternat&(e
>>>def twice(number): Finds the twice of the number . int answer answer=2*number return answer

>>>def twice(number): Finds the twice of the number . int answer answer=2*number return answer

def de#ines a #unction numbers is 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 return it twice(101) 3ill return *6*%

FRUITFU+ and -OI, FUNCTIONS


So!e o# t$e #unctions yield results 3$ile so!e not%

For e%g% & 777 type,)*8 type 9 int 9 7

*)is is a void 'unction as it not return any value.

For e. . : def twice(number): Finds the twice of the number int answer answer=2*number return answer

!his function returns a "a#ue bac$ to the main. %o this a fruitfu# function.

E!ec#t&on of f#nct&ons
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!ents t$ere( and t$en co!es 0ac2 to pic2 up 3$ere it le#t o##%

%h' f#nct&ons$
< =e can na!e a group o# state!ents( 3$ic$ !a2es our progra! easier to read and de+ug% Functions can !a2e a program smaller 0y eli!inating repetiti+e code% >ater( i# you !a2e a c$ange( you only $a+e to !a2e it in one place% =ell1designed #unctions are o#ten use#ul #or !any progra!s% Once you 3rite and de0ug one( you can reuse it%

Nested F#nct&ons

=$en a #unction calls anot$er #unction%

For e. . : def twice(number): Finds the twice of the number int answer answer=2*number return answer def twice&s'uare(number): Finds the s'uare of twice of the number int t int answer t=twice(number) answer=t*t return answer
777t3ice?s"uare,* 64@66

RECURSION
*)e a+ility o' a 'unction to call itsel' is called ,%-.,/012.

For e"g" . def co#ntdown/n0. &f n 12 3. pr&nt 4* astoff54 e se. pr&nt n co#ntdown/n670

) * 4 Blasto##A

You might also like