objec ves
play

Objec(ves Defining your own func(ons Control flow Scope, variable - PDF document

Objec(ves Defining your own func(ons Control flow Scope, variable life(me Tes(ng Refining our development process BI: Net Neutrality Feb 2, 2018 Sprenkle - CSCI111 1 Review What are benefits of func(ons? What is the


  1. Objec(ves • Defining your own func(ons Ø Control flow Ø Scope, variable life(me • Tes(ng • Refining our development process • BI: Net Neutrality Feb 2, 2018 Sprenkle - CSCI111 1 Review • What are benefits of func(ons? • What is the syntax for crea(ng a func(on? • What is the special keyword that means “this is the output for the func(on”? Feb 2, 2018 Sprenkle - CSCI111 2 1

  2. Review: Syntax of Func(on Defini(on Input Name/ Keyword Func-on Parameter Name def def average2(num1, num2): Func-on header """ """ (or func-on defini-on) Parameters: two numbers to be averaged. Parameters: two numbers to be averaged. Returns the average of two numbers Returns the average of two numbers Func-on documenta-on """ """ average = (num1 + num2)/2 Body return return average Output Keyword: How to give output Feb 2, 2018 Sprenkle - CSCI111 3 Review: Calling your own func(ons Same as calling someone else’s functions … average = average2(100, 50) Output is Input Func-on assigned to Name average average average2.py Feb 2, 2018 Sprenkle - CSCI111 4 2

  3. Review: Func(on Output • When the code reaches a statement like return return x Ø The func(on stops execu(ng Ø x is the output returned to the place where the func(on was called • For func(ons that don't have explicit output, return does not have a value with it, e.g., return return • Op(onal: don't need to have return Ø Func(on automa)cally returns at the end Feb 2, 2018 Sprenkle - CSCI111 5 Words in Different Contexts “Time flies like an arrow. Fruit flies like bananas.” — Groucho Marx. • Output from a func/on Ø What is returned from the func(on Ø If the func(on prints something, it’s what the func(on displays (rather than outputs). • Output from a program Ø What is displayed by the program Feb 2, 2018 Sprenkle - CSCI111 6 3

  4. return vs print • In general, whenever we want output from a func(on, we’ll use return Ø More flexible, reusable func(on Ø Let whoever called the func(on figure out what to display • Use print for Ø Debugging your func(on (then remove) • Otherwise, unintended side effect of calling the func(on Ø When you have a func(on that is supposed to display something • Some(mes, that is what you want. Feb 2, 2018 Sprenkle - CSCI111 7 Review: Program Organiza(on • Larger programs require func/ons to maintain readability Ø Use main main() () and other func(ons to break up program into smaller , more manageable chunks Ø “ Abstract away” the details • As before, can s(ll write smaller scripts without any func(ons Ø Can try out func(ons using smaller scripts • Need the main main() () func(on when using other func(ons to keep “driver” at top Ø Otherwise, func(ons need to be defined before use Feb 2, 2018 Sprenkle - CSCI111 8 4

  5. Review: Example program with a main() def main(): printVerse("dog", "ruff") Constants, comments printVerse("duck", "quack") are in example program animal_type = "cow" animal_sound = "moo" printVerse(animal_type, animal_sound) def printVerse(animal, sound): print(BEGIN_END + EIEIO) print("And on that farm he had a " + animal + EIEIO) print("With a " + sound + ", " + sound + " here") print("And a " + sound + ", " + sound + " there") print("Here a", sound) print("There a", sound) print("Everywhere a " + sound + ", " + sound) print(BEGIN_END + EIEIO) print() In what order does this program execute? What is output from this program? main() Feb 2, 2018 Sprenkle - CSCI111 9 oldmac.py Review: Example program with a main() 4 1. Set definition of main def main(): printVerse("dog", "ruff") 2. Set definition of printVerse printVerse("duck", "quack") 1 3. Call main function 4. Execute main function animal_type = "cow" animal_sound = "moo" 5. Call, execute printVerse printVerse(animal_type, animal_sound) … 5 def printVerse(animal, sound): print(BEGIN_END + EIEIO) print("And on that farm he had a " + animal + EIEIO) print("With a " + sound + ", " + sound + " here") 2 print("And a " + sound + ", " + sound + " there") print("Here a", sound) print("There a", sound) print("Everywhere a " + sound + ", " + sound) print(BEGIN_END + EIEIO) print() oldmac.py main() 3 Feb 2, 2018 Sprenkle - CSCI111 10 5

  6. VARIABLE LIFETIMES AND SCOPE Feb 2, 2018 Sprenkle - CSCI111 11 What does this program output? def def main(): 
 x = 10 sum = sumEvens( x ) print("The sum of even #s up to", x, "is", sum) def def sumEvens(limit): total = 0 for for x in in range(0, limit, 2): total += x return total return main() mystery.py Feb 2, 2018 Sprenkle - CSCI111 12 6

  7. Func(on Variables def def main(): 
 x = 10 sum = sumEvens( x ) print("The sum of even #s up to", x, "is", sum) def sumEvens(limit): def total = 0 for x in for in range(0, limit, 2): total += x return return total Why can we name two different variables x? main() mystery.py Feb 2, 2018 Sprenkle - CSCI111 13 Tracing through Execu(on When you call main (), that means you def def main(): 
 x = 10 want to execute this function Defines functions sum = sumEvens( x ) print("The sum of even #s up to", x, "is", sum) def def sumEvens(limit): total = 0 for x in for in range(0, limit, 2): total += x return return total main() Feb 2, 2018 Sprenkle - CSCI111 14 7

  8. Func(on Variables def def main() : 
 x = 10 sum = sumEvens( x ) print("The sum of even #s up to", x, "is", sum) def def sumEvens(limit) : total = 0 for x in for in range(0, limit, 2): total += x return return total Variable names � Memory stack are like first names main() main x 10 Function names are like last names Define the SCOPE of the variable Feb 2, 2018 Sprenkle - CSCI111 15 Func(on Variables def def main() : 
 x = 10 sum = sumEvens( x ) print("The sum of even #s up to", x, "is", sum) Called the function sumEvens def def sumEvens(limit) : total = 0 Add its parameters to the stack for for x in in range(0, limit, 2): total += x sum Evens limit 10 return return total main() main x 10 Feb 2, 2018 Sprenkle - CSCI111 16 8

  9. Func(on Variables def def main() : 
 x = 10 sum = sumEvens( x ) print("The sum of even #s up to", x, "is", sum) def def sumEvens(limit) : total = 0 for for x in in range(0, limit, 2): total += x sum total 0 return return total Evens limit 10 main() main x 10 Feb 2, 2018 Sprenkle - CSCI111 17 Func(on Variables def def main() : 
 x = 10 sum = sumEvens( x ) print("The sum of even #s up to", x, "is", sum) def sumEvens(limit) : def total = 0 for for x in in range(0, limit, 2): x 0 total += x sum total 0 return total return Evens limit 10 main() main x 10 Feb 2, 2018 Sprenkle - CSCI111 18 9

  10. Func(on Variables def def main() : 
 x = 10 sum = sumEvens( x ) print("The sum of even #s up to", x, "is", sum) def def sumEvens(limit) : total = 0 for x in for in range(0, limit, 2): x 8 total += x sum total 20 return return total Evens limit 10 main() main x 10 Feb 2, 2018 Sprenkle - CSCI111 19 Func(on Variables def def main() : 
 x = 10 sum = sumEvens( x ) print("The sum of even #s up to", x, "is", sum) def sumEvens(limit) : def total = 0 Function sumEvens returned for for x in in range(0, limit, 2): • no longer have to keep track of total += x its variables on stack return return total • lifetime of those variables is over main() sum 20 main x 10 Feb 2, 2018 Sprenkle - CSCI111 20 10

  11. Func(on Variables def def main() : 
 x = 10 sum = sumEvens( x ) print("The sum of even #s up to", x, "is", sum) def def sumEvens(limit) : total = 0 for for x in in range(0, limit, 2): total += x return return total main() x 10 main sum 20 Feb 2, 2018 Sprenkle - CSCI111 21 Variable Scope • Func(ons can have the same parameter and variable names as other func(ons Ø Need to look at the variable’s scope to determine which one you’re looking at Ø Use the stack to figure out which variable you’re using • Scope levels Ø Local scope (also called func/on scope ) • Can only be seen within the func(on Ø Global scope (also called file scope ) • Whole program can access • More on these later Feb 2, 2018 Sprenkle - CSCI111 22 11

  12. Summary: Why Write Func(ons? • Allows you to break up a hard problem into smaller , more manageable parts • Makes your code easier to understand • Hides implementa(on details ( abstrac)on ) Ø Provides interface (input, output) • Makes part of the code reusable so that you: Ø Only have to write func(on code once Ø Can debug it all at once • Isolates errors Ø Can make changes in one func(on ( maintainability ) Similar to benefits of OO Programming Feb 2, 2018 Sprenkle - CSCI111 23 TESTING FUNCTIONS Feb 2, 2018 Sprenkle - CSCI111 24 12

Recommend


More recommend