Python STARTING OUT WITH Chapter 3 Simple Functions

Download Report

Transcript Python STARTING OUT WITH Chapter 3 Simple Functions

Chapter 3
Simple Functions
STARTING OUT WITH
Python
First Edition
by
Tony Gaddis
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3.1 Introduction to Functions
Concept:
A function is a group of statements
that exists within a program for the
purpose of performing a specific task.
1-2
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-2
3.1 Introduction to Functions
Divide and Conquer
Figure 3-1 Using
functions to divide and
conquer a large task
1-3
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-3
3.1 Introduction to Functions
Benefits of Using Functions
•
•
•
•
•
Simple Code
Code Reuse
Better Testing
Faster Development
Easier Facilitation of Teamwork
1-4
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-4
3.2 Defining and Calling a Function
Concept:
The code for a function is known as a
function definition. To execute the
function, you write a statement that
calls it.
1-5
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-5
3.2 Defining and Calling a Function
Function Names
•Cannot use Python key words
•Cannot contain spaces
•First character must be one of the letters a through z,
A through Z, or an underscore character(_)
•After the first character use letters a through z, A
through Z, digits 0 through 9, or underscore
•Uppercase and lowercase characters are distinct
1-6
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-6
3.2 Defining and Calling a Function
Defining and Calling a Function
Function header
Block
def function_name():
statement
statement
etc.
1-7
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-7
3.2 Defining and Calling a Function
Defining and Calling a Function
Figure 3-2 The function definition and the function call
1-8
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-8
3.2 Defining and Calling a Function
Indentation in Python
Figure 3-7 All of the statements in a block are indented
1-9
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-9
3.3 Designing a Program to Use Functions
Concept:
Programmers commonly use a
technique known as top-down design
to break down an algorithm into
functions.
1-10
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-10
3.3 Designing a Program to Use Functions
Flowcharting a Program with Functions
Function Call
Figure 3-8 Function call symbol
1-11
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-11
3.3 Designing a Program to Use Functions
Flowcharting a Program with Functions
Figure 3-9 Flowchart for Program 3-2
1-12
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-12
3.3 Designing a Program to Use Functions
Top-Down Design
• The overall task of the program is broken
down into a series of subtasks
• Each of the subtasks is examined to determine
whether it can be further broken down into
more subtasks.
• Once the subtasks are identified, they are
written in code.
1-13
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-13
3.3 Designing a Program to Use Functions
Pausing Execution Until the User Presses
Enter
raw_input(‘Press Enter to see Step 1.’)
1-14
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-14
3.4 Local Variables
Concept:
A local variable is created inside a function
and cannot be accessed by statements that
are outside the function. Different functions
can have local variables with the same
names because the functions cannot see
each others’ local variables.
1-15
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-15
3.4 Local Variables
Scope and Local Variables
A local variable’s scope is the function in
which the variable is created
1-16
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-16
3.4 Local Variables
Program 3-5 (birds.py)
1-17
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-17
3.5 Passing Arguments to Functions
Concept:
An argument is any piece of data that is
passed into a function when the function is
called. A parameter is a variable that
receives an argument that is passed into a
function.
1-18
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-18
3.5 Passing Arguments to Functions
Program 3-6 (pass_arg.py)
1-19
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-19
3.5 Passing Arguments to Functions
Figure 3-13 The value
variable is passed as an
argument
Figure 3-14 The value variable and
the number parameter reference the
same value
1-20
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-20
3.5 Passing Arguments to Functions
Parameter Variable Scope
•A parameter variable’s scope is the function in which
the parameter is used.
•Inside the function, the parameter variable can be
accessed, but no statement outside the function can
access it.
1-21
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-21
3.5 Passing Arguments to Functions
Passing Multiple Arguments
Program 3-8 (multiple_args.py)
1-22
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-22
3.5 Passing Arguments to Functions
Making Changes to Parameters
Program 3-10 (change_me.py)
1-23
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-23
3.5 Passing Arguments to Functions
Keyword Arguments
Specifies which parameter the argument should be
passed into. Its position in the function call does not
matter.
Program 3-11
(keyword_args.py)
1-24
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-24
3.6 Global Variables and Global Constants
Concept:
A global variable is accessible to all the
functions in a program file.
1-25
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-25
3.6 Global Variables and Global Constants
Program 3-13
(global1.py)
1-26
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-26
3.6 Global Variables and Global Constants
Restrict the use of global variables for the
following reasons:
• Global variables make debugging difficult
• Functions that use global variables are
usually dependent on those variables.
• Global variables make a program hard to
understand.
1-27
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-27
3.6 Global Variables and Global Constants
Global Constants
It is a global name that references a value
that cannot be changed during the
program’s execution.
1-28
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3-28
Chapter 3
Simple Functions
QUESTIONS
?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley