You are on page 1of 15
Paogiess ire ieee srg Building our Own Functions + We create a new function using the def keyword followed by optional parameters in parentheses + We indent the body of the function * This defines the function but does not execute the body of the function def print_lyrics(): oe GP aE ee MPR tat Coe Cae a cee) POSE GP ee TOME TC CAE Wb a (cs a tak” \ ee) Paogiess ire ieee srg print lyrics(): J printtt'ma lumberjack, and I'm okay Panes ee eMac Roe eke es x=5 print (‘Hello’) Cae set ane stort Oe OSE AGR aE MWB Rr ter) Se Mea Sc Nae) ee} Sea Gr eC Tere Cart Means eee Cae) va print ('¥o') if eee ar print (x) ire ieee Definitions and Uses * Once we have defined a function, we can call (or invoke) it Mur laa Rom Lod * This is the store and reuse pattern Paogiess Baran Paogiess ire ieee srg x=5 print ('Hello') def print_lyrics(): See ACR ar MMR tt aE Sa Tea sae) SRC AGr ats ee VeMe Ee Ca Mea So eSB Camby print('Yo') print_lyrics() eas print (x) Lal-il[e) Yo I'm a lumberjack, and I'm okay. | sleep all night and | work all day. v Paogiess ire ieee srg Arguments + An argument is a value we pass into the function as its input aera UT nCes Co) A) + We use arguments so we can direct the function to do different kinds of work when we call it at different times + We put the arguments in parentheses after the name of the function big = max('Hello world’) Argument ire ieee Paogiess Parameters Poe Cr ie CCE a Peep) ae peek On CPE OD) A parameter is a variable which we use Cortese ae sas Tam nm iUl ares Colao ai alo g ea LS) print ('Bonjour') Pron “handle” that allows the code in the function to access the arguments fora ace poe errata) particular function invocation. coer pee Orin) BOTY peor Gs a) PSs >> cose tter-CoBr IB) Paogiess ire ieee srg Return Values Often a function will take its arguments, do some computation, and return a value to be used as the value of the function call in the calling expression. The return keyword is used for this. def greet(): Per Mas CS BR Hello Glenn Hello Sally print(greet(), "Glenn") Porta Cras 16 Tea) ire ieee Paogiess Baran Return Value + A “fruitful” function is one that produces a result (or return value) SR MhU lea CLC a1) the function execution and “sends back” the result of the function Foe ae Crier Cta et) 5 Pree earn pats oe reeset roa Pers Ee total Orn PCr ECS >>> print (greet (‘en’), 'Glenn') Hello Glenn >>> print (greet (‘es'), 'Sally') Hola Sally >>> print (greet ('£r'), 'Michael') Serrano pood Paogiess Baran Arguments, Parameters, and Results Poo SK me rt Tas: CURR ME (oa EB) eles >>> print (big) w ire ieee Cae tren Sed , , Sey i Hello world’ >| gor x in inp: | ——> | 'W wa blah Sey rele -ias poo a eens Paogiess ire ieee srg Multiple Parameters / Arguments + We can define more than one def addtwo(a, b): Feyenoord Ct ad definition petateasE eI Cel , x = addtwo(3, 5) + We simply add more arguments print (x) when we call the function 8 Cea N-Male Clam Ul -M alla) el-lar- ale Rote (-e of arguments and parameters Paogiess ire ieee srg Void (non-fruitful) Functions ° When a function does not return a value, we call it a “void” function bode at Talo COLA Mae Col UTA] APTL UP Mm UTA el (ogy bam ooo iV are Colne MnTo Lm e Paogiess ire ieee srg To function or not to function... + Organize your code into “paragraphs” - capture a complete thought and “name it” + Don’t repeat yourself - make it work once and then reuse it + If something gets too long or complex, break it up into logical chunks and put those chunks in functions + Make a library of common stuff that you do over and over - perhaps share this with your friends... Paogiess ire ieee Baran Summary Cece (O lait tn) Cav areit(elatc} SUNCOM PiU MiP LaTer Ce lRIS)) + Built-In Functions + Void (non-fruitful) functions — Type conversion (int, float) + Why use functions? — String conversions Oe rT) Paogiess ire ieee srg Exercise Rewrite your pay computation with time-and-a-half for overtime and create a function called computepay which takes two parameters ( hours and rate). Enter Hours: 45 inl Cm ate l oa) Pay: 475.0 475 =40*10+5*15 ” nae atte oie ted srg Acknowledgements / Contributions eee Ter gee ca) r ene De rucro ny oer) ‘and made available under a Coe ee cme eee Pee ees ere ee eur ea ee eet etd Contributors on this page as you republish the materials, Ce ee Seine ay Ce oe EOC os

You might also like