Python Programming, 1/e 1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions Python Programming, 1/e 2 Objectives ? To 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, 1/e 3 Objectives (cont.) ? To write programs that use functions to reduce code duplication and increase program modularity. Python Programming, 1/e 4 The Function of Functions ? So far, we ’ ve seen four different types of functions: ? Our prise a single function called main(). ? Built-in Python functions (abs) ? Functions from the standard libraries () ? Functions from the graphics module (()) Python Programming, 1/e 5 The Function of Functions ? In the code on page 166, the code for drawing the bars occurs in two different places. ? The first bar is drawn immediately before the loop. ? The remaining bars are drawn within the loop. Python Programming, 1/e 6 The Function of Functions ? Having 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, 1/e 7 Functions, Informally ? A 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, 1/e 8 Functions, Informally ? The 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, 1/e 9 Functions,
计算机编程简介 来自淘豆网www.taodocs.com转载请标明出处.