FoP Ch03 Functions
FoP Ch03 Functions
Programming
W4 - Functions
1
● Best way to develop and maintain a large program is to
construct it from smaller, more manageable pieces →
divide and conquer.
● Using existing functions as building blocks for creating
new programs is a key aspect of software reusability
○ Allows to execute function from various locations in program just by
calling the function, rather than duplicating the possibly lengthy code
○ Makes programs easier to modify
2
Defining Functions first line in a function’s block
should be a docstring that
briefly explains the function’s
def keyword, followed by the parentheses contain the
purpose
function name (square) function’s parameter list
# 1- Perform a task
# 2- Return a value 3
return
4
What Happens When You Call a Function
1. The expression square(7) passes the argument 7 to
square’s parameter number.
2. Then square calculates number ** 2 and returns the result.
a. The parameter number
■ exists only during the function call
■ created on each call to the function to receive the argument value
■ destroyed when the function returns its result to the caller.
5
Function’s parameters and variables
6
Accessing a Function’s Docstring via
IPython’s Help Mechanism
7
Functions with Multiple Parameters
8
Python’s Built-In max and min Functions
Using built-in functions or functions from the Python Standard Library’s modules
→ can reduce development time
→ and increase program reliability, portability and performance.
For a list of Python’s built-in functions and modules, see
https://docs.python.org/3/library/index.html
9
Random-Number Generation
10
fig04_01.py
Roll a six-
sided die
6,000,000
times
11
Seeding the Random-Number Generator for
Reproducibility
module’s seed function
to seed the random-
number generator
yourself—this forces
randrange to begin
calculating its
pseudorandom number
sequence from the seed
you specify
12
Case Study: A Game of Chance
13
● Returning multiple
values via a Tuple
● Tuple is an
immutable (that is,
unmodifiable)
sequences of values
● Parentheses are
optional, but we
recommend using
them for clarity
14
● The in operator tests
whether the tuple (7, 11)
contains
sum_of_dice’s value
● not in operator to
determine whether a
value is not in an
iterable.
15
Python Standard Library
16
17
math Module Functions
18
https://docs.python.org/3/library/math.html
19
Using IPython Tab Completion for Discovery
20
Default Parameter Values
21
Arbitrary Argument Lists
22
Passing an Iterable’s Individual Elements as
Function Arguments
23
Methods: Functions That Belong to Objects
24
Scope Rules
25
Accessing a Global Variable from a Function
26
by default, you cannot modify
a global variable in a
function—when you first
assign a value to a variable in
a function’s block, Python
creates a new local variable
27
use global statement to
declare the variable is
defined in global scope
28
Blocks vs. Suites
29
Shadowing Functions
30
import: A Deeper Look
31
Caution: Avoid Wildcard Imports
32
Binding Names for Modules and Module
Identifiers
33
Passing Arguments to Functions: A
Deeper Look
In many programming languages, there are two ways to pass arguments—pass-by-value
and pass-by-reference (sometimes called call-by-value and call-by-reference,
respectively):
● With pass-by-value, the called function receives a copy of the argument’s value and
works exclusively with that copy. Changes to the function’s copy do not affect the
original variable’s value in the caller.
● With pass-by-reference, the called function can access the argument’s value in the
caller directly and modify the value if it’s mutable.
When a function call provides an argument, Python copies the argument object’s
reference—not the object itself—into the corresponding parameter
34
Memory Addresses, References and
“Pointers”
● Interact with an object via a reference → object’s address
(or location) in the computer’s memory—sometimes called
a “pointer” in other languages
35
Built-In Function id and Object Identities
36
Passing an Object to a Function
37
Testing Object Identities with the is
Operator
38
Immutable Objects as Arguments
immutable (unmodifiable) object—such as an int, float, string or tuple
→ cannot modify the original immutable object’s value
40
What vs. How
41
Pure Functions
42
Intro to Data Science: Measures of Dispersion
43
Using the following population of 10 six-sided die rolls:
population variance = Squaring the difference between each die value and the mean of all
die values
standard deviation = the square root of the variance
Closer the data values are to the mean and the less overall dispersion (that is, spread)
there is between the values and the mean.
44