we are past the middle, instead of wasting time in a midterm, lets do a
quick quiz
every class from now to
the end. short and simple. derived from homeworks.
what's next:
1. functions
2. recursion
3. strings
4. tuples
5. lists
today lets go over two simple exercises from chapter 7:
7.3
7.6
any other exercise from ch 7 or past due exercises you want to do?
CHAPTER 8
Functions
how to write your own functions and modules
def CelsiusToFarenheit (x):
return 9 * x / 5 + 32
the code inside the function definition is executed when the function is called
Suppose file divideByZero.py contains:
def foo ():
return 1/0
what happens when you execute:
python3 divideByZero.py
nothing because 1/0 is not executed
the function has not been called
what if file divideByZero.py contains:
def foo ():
return 1/0
foo()
what happens now when you execute:
python3 divideByZero.py
This
preview
has intentionally blurred sections.
Sign up to view the full version.
now it crashes because 1/0 is executed
Encapsulation
: Wrapping up a piece of useful code in such a way that it can be used without knowledge
of the specifics. Black box.
Generalization
: Making a piece of code useful for a variety of circumstances by controlling it via
parameters. Convert x degrees Farenheit to Celsius.
Manageability
: Dividing a complex program into easy-to-manage chunks. Divide and conquer.
Maintainability
: Using meaningful names and logical wrappings to make a program better readable and
understandable
Reusability
: Facilitating the transfer of functionalities between programs
Recursion
: Allowing the use of a technique called "recursion" (Chapter 9). A function calls itself. Some
languages do not have loops but achieve the same results using only recursion.
wht happens when this code is executed?
CelsiusToFarenheit(31)
def CelsiusToFarenheit (x):
return 9 * x / 5 + 32
this is an error because the function is called before it is defined.

This is the end of the preview.
Sign up
to
access the rest of the document.
- Summer '17
- Recursion
-
Click to edit the document details