Python Programming: An Introduction to Computer ScienceObjectivesObjectives (cont.)The Function of FunctionsWhy use functions?Slide 6Functions, InformallySlide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Future Value with a FunctionSlide 19Slide 20Functions: The DetailsSlide 22Slide 23ScopeScope continuedSlide 26Slide 27Slide 28Slide 29Function Execution: The DetailsSlide 31Functions: The DetailSlide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Functions and Parameters: The DetailsGetting Results from a FunctionFunctions That Return ValuesSlide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Calling (invoking) a functionExamples of callsOrder of function definitionsSome important points about function call semanticsSlide 56Classified by LocationArguments / ParametersA Parameter or a Local Variable?Functions that Modify ParametersSlide 61Slide 62Slide 63Slide 64Slide 65Slide 66Slide 67Slide 68Slide 69Slide 70Slide 71Slide 72Slide 73Slide 74Slide 75Slide 76Slide 77Slide 78Slide 79Slide 80Slide 81Slide 82Slide 83Slide 84Slide 85Slide 86Functions and Program StructureDocument your functions!Slide 89QuestionsMore QuestionsSlide 92Slide 93Slide 94Slide 95Python 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 argument 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())Why use functions?easier for programmers to work togetherput details off while looking at big pictureeasier to reuse codeeasier testing and debuggingeasier to focus on one module at a timePython Programming, 2/e 6The 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 7Functions, InformallyA function is like a subprogram, a small program that is part of a larger one.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, like making "new keywords" in the languagePython Programming, 2/e 8Functions, 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 9Functions, 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 10Functions, 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 11Functions, 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 12Functions, 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 13Functions, 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 14Functions, 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 15Functions, InformallyThe generic function singdef sing(person): happy() happy() print("Happy birthday, dear", person + ".“) happy()This function uses a parameter named person. A parameter is a variable that holds a place for a value. It is initialized with the argument value when the function is called.Python Programming, 2/e 16Functions, 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 17Functions, 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 18Future 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 19Future 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
View Full Document