Python Programming: An Introduction to Computer ScienceObjectivesObjectives (cont.)The Function of FunctionsSlide 5Functions, InformallySlide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Future Value with a FunctionSlide 18Slide 19Functions and Parameters: The DetailsSlide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Functions and Parameters: The DetailSlide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Functions and Paramters: The DetailsSlide 37Slide 38Getting Results from a FunctionFunctions That Return ValuesSlide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Functions that Modify ParametersSlide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59Slide 60Slide 61Slide 62Slide 63Slide 64Slide 65Slide 66Slide 67Slide 68Slide 69Slide 70Slide 71Slide 72Slide 73Slide 74Functions and Program StructureSlide 76Slide 77Slide 78Slide 79Python Programming, 2/e 1Python Programming:An Introduction to Computer ScienceChapter 6Defining FunctionsPython Programming, 2/e 2ObjectivesTo understand why programmers divide programs up into sets of cooperating functions.To be able to define new functions in Python.To understand the details of function calls and parameter passing in Python.Python Programming, 2/e 3Objectives (cont.)To write programs that use functions to reduce code duplication and increase program modularity.Python Programming, 2/e 4The Function of FunctionsSo far, we’ve seen four different types of functions:Our programs comprise a single function called main().Built-in Python functions (abs)Functions from the standard libraries (math.sqrt)Functions from the graphics module (p.getX())Python Programming, 2/e 5The Function of FunctionsHaving similar or identical code in more than one place has some drawbacks.Issue one: writing the same code twice or more.Issue two: This same code must be maintained in two separate places.Functions can be used to reduce code duplication and make programs more easily understood and maintained.Python Programming, 2/e 6Functions, InformallyA function is like a subprogram, a small program inside of a program.The basic idea – we write a sequence of statements and then give that sequence a name. We can then execute this sequence at any time by referring to the name.Python Programming, 2/e 7Functions, InformallyThe part of the program that creates a function is called a function definition.When the function is used in a program, we say the definition is called or invoked.Python Programming, 2/e 8Functions, InformallyHappy Birthday lyrics…def main(): print("Happy birthday to you!" ) print("Happy birthday to you!" ) print("Happy birthday, dear Fred...") print("Happy birthday to you!")Gives us this…>>> main()Happy birthday to you!Happy birthday to you!Happy birthday, dear Fred...Happy birthday to you!Python Programming, 2/e 9Functions, InformallyThere’s some duplicated code in the program! (print("Happy birthday to you!"))We can define a function to print out this line:def happy(): print("Happy birthday to you!")With this function, we can rewrite our program.Python Programming, 2/e 10Functions, InformallyThe new program – def singFred(): happy() happy() print("Happy birthday, dear Fred...") happy()Gives us this output –>>> singFred()Happy birthday to you!Happy birthday to you!Happy birthday, dear Fred...Happy birthday to you!Python Programming, 2/e 11Functions, InformallyCreating this function saved us a lot of typing!What if it’ s Lucy’s birthday? We could write a new singLucy function!def singLucy(): happy() happy() print("Happy birthday, dear Lucy...") happy()Python Programming, 2/e 12Functions, InformallyWe could write a main program to sing to both Lucy and Freddef main(): singFred() print() singLucy()This gives us this new output>>> main()Happy birthday to you!Happy birthday to you!Happy birthday, dear Fred..Happy birthday to you!Happy birthday to you!Happy birthday to you!Happy birthday, dear Lucy...Happy birthday to you!Python Programming, 2/e 13Functions, InformallyThis is working great! But… there’s still a lot of code duplication.The only difference between singFred and singLucy is the name in the third print statement.These two routines could be collapsed together by using a parameter.Python Programming, 2/e 14Functions, InformallyThe generic function singdef sing(person): happy() happy() print("Happy birthday, dear", person + ".“) happy()This function uses a parameter named person. A paramater is a variable that is initialized when the function is called.Python Programming, 2/e 15Functions, InformallyOur new output –>>> sing("Fred")Happy birthday to you!Happy birthday to you!Happy birthday, dear Fred.Happy birthday to you!We can put together a new main program!Python Programming, 2/e 16Functions, InformallyOur new main program:def main(): sing("Fred") print() sing("Lucy")Gives us this output:>>> main()Happy birthday to you!Happy birthday to you!Happy birthday, dear Fred.Happy birthday to you!Happy birthday to you!Happy birthday to you!Happy birthday, dear Lucy.Happy birthday to you!Python Programming, 2/e 17Future Value with a FunctionIn the future value graphing program, we see similar code twice:# Draw bar for initial principalbar = Rectangle(Point(0, 0), Point(1, principal))bar.setFill("green")bar.setWidth(2)bar.draw(win) bar = Rectangle(Point(year, 0), Point(year+1, principal))bar.setFill("green")bar.setWidth(2)bar.draw(win)Python Programming, 2/e 18Future Value with a FunctionTo properly draw the bars, we need three pieces of information.The year the bar is forHow tall the bar should beThe window the bar will be drawn inThese three values can be supplied as parameters to the function.Python Programming, 2/e 19Future Value with a FunctionThe resulting function looks like this:def drawBar(window, year, height): # Draw a bar in window starting at year with given height bar = Rectangle(Point(year, 0), Point(year+1, height)) bar.setFill("green") bar.setWidth(2) bar.draw(window)To use this function, we supply the three values. If win is a Graphwin, we can draw a bar for year 0 and principal of $2000 using this call:drawBar(win, 0, 2000)Python Programming, 2/e 20Functions and Parameters: The DetailsIt makes sense to include the year and the
View Full Document