an introduction to python - functions, part 1

22
An Introduction To Software Development Using Python Spring Semester, 2014 Class #17: Functions, Part 1

Upload: blue-elephant-consulting

Post on 17-Jul-2015

403 views

Category:

Education


7 download

TRANSCRIPT

Page 1: An Introduction To Python - Functions, Part 1

An Introduction To Software Development

Using Python

Spring Semester, 2014

Class #17:Functions, Part 1

Page 2: An Introduction To Python - Functions, Part 1

The Worst Homework Assignment EVER!

• Using the 15 sets of patient data that you’ve been given in Homework #2, sort each one of them in ascending order based on each of the 11 fields in a record.

Image Credit: luluscottage.blogspot.com

Page 3: An Introduction To Python - Functions, Part 1

2nd Worst Homework Assignment EVER!

• Now, using the 15 sets of patient data that you’ve been given in Homework #2, sort each one of them in DECENDING order based on each of the 11 fields in a record.

Image Credit: www.clipartpanda.com343 × 350

Page 4: An Introduction To Python - Functions, Part 1

We Have A Problem

• We are running a VERY successful ice cream store.

• Creating an ice cream cone is a 3-step process:1. Select flavor of ice cream2. Construct cone3. Add toppings

• As the store owner we have two problems: consistent quality, minimizing training time.

Image Credit: www.etsy.com

Page 5: An Introduction To Python - Functions, Part 1

I Got 99 Problems But Quality Ain't One

• Select flavor of ice cream– Check inventory for available flavors– Separate ice cream from yogurt– Get user selection

• Construct cone– Select cone type: waffle, sugar, regular– Determine # of scoops: 1, 2, 3, crazy

• Add toppings– Check inventory for available flavors– Separate into regular and premium toppings– Get user selection

Image Credit: modresdes.com

Page 6: An Introduction To Python - Functions, Part 1

Training Is Going To Be A Pain

1. Check inventory for available flavors2. Separate ice cream from yogurt3. Get user selection4. Select cone type: waffle, sugar, regular5. Determine # of scoops: 1, 2, 3, crazy6. Check inventory for available flavors7. Separate into regular and premium toppings8. Get user selection

Image Credit: www.gograph.com

Page 7: An Introduction To Python - Functions, Part 1

What Is Our Python Code Going To Look Like?

# drive through orderCheck inventory for available flavorsSeparate ice cream from yogurtGet user selectionSelect cone type: waffle, sugar, regularDetermine # of scoops: 1, 2, 3, crazyCheck inventory for available flavorsSeparate into regular and premium toppingsGet user selection

# walk-up orderCheck inventory for available flavorsSeparate ice cream from yogurtGet user selectionSelect cone type: waffle, sugar, regularDetermine # of scoops: 1, 2, 3, crazyCheck inventory for available flavorsSeparate into regular and premium toppingsGet user selection

# online orderCheck inventory for available flavorsSeparate ice cream from yogurtGet user selectionSelect cone type: waffle, sugar, regularDetermine # of scoops: 1, 2, 3, crazyCheck inventory for available flavorsSeparate into regular and premium toppingsGet user selection

# phone-in orderCheck inventory for available flavorsSeparate ice cream from yogurtGet user selectionSelect cone type: waffle, sugar, regularDetermine # of scoops: 1, 2, 3, crazyCheck inventory for available flavorsSeparate into regular and premium toppingsGet user selection

Image Credit: pixshark.com

Page 8: An Introduction To Python - Functions, Part 1

There Has To Be A Better Way To Make Ice Cream

Cones!• In Python, a function packages a computation consisting of multiple steps

into a form that can be easily understood and reused.– e.g. Select flavor of ice cream, Construct cone, Add toppings

• A function is a sequence of instructions with a name.

• You call a function in order to execute its instructions. For example, consider the following program statement:

price = round(6.8275, 2) # Sets result to 6.83

Image Credit: www.clipartpanda.com

Page 9: An Introduction To Python - Functions, Part 1

What Happens When You Call A Function?

price = round(6.8275, 2)

“Arguments”

Note: Multiple argumentscan be passed to a function.Only one value can be returned.

Page 10: An Introduction To Python - Functions, Part 1

Benefits Of Using Functions

• The first reason is reusability. Once a function is defined, it can be used over and over and over again. You can invoke the same function many times in your program, which saves you work.

• A single function can be used in several different programs. When you need to write a new program, you can go back to your old programs, find the functions you need, and reuse those functions in your new program.

• The second reason is abstraction. If you just want to use the function in your program, you don't have to know how it works inside! You don't have to understand anything about what goes on inside the function.

Page 11: An Introduction To Python - Functions, Part 1

How To Create A Function

• When writing this function, you need to– Pick a name for the function (selectFlavor).– Define a variable for each argument (yogurt).

• These variables are called the parameter variables.

• Put all this information together along with the def reserved word to form the first line of the function’s definition:

def selectFlavor(yogurt) :

• This line is called the header of the function.

Image Credit: imgarcade.com

Page 12: An Introduction To Python - Functions, Part 1

Create The Body Of The Function

• The body contains the statements that are executed when the function is called.

listOfFlavors = checkInventory()while flavor in listOfFlavors print(listOfFlavors[flavor])

if (yogurt) : selection = input(“Please enter the flavor of yogurt you would like:”)else : selection = input(“Please enter the flavor of ice cream you would like:”)

Image Credit: www.pinterest.com

Page 13: An Introduction To Python - Functions, Part 1

Send The Result Back

• In order to return the result of the function, use the return statement:

return selection

Image Credit: www.clipartpanda.com

Page 14: An Introduction To Python - Functions, Part 1

Final Form Of Our Function

def selectFlavor(yogurt) :listOfFlavors = checkInventory()while flavor in listOfFlavors print(listOfFlavors[flavor])

if (yogurt == 1) : selection = input(“Please enter the flavor of yogurt you would like:”)else : selection = input(“Please enter the flavor of ice cream you would like:”)

return selection

Note: A function is a compound statement, which requires the statements in the body to be indented to the same level.

Image Credit: www.clipartpanda.com

Page 15: An Introduction To Python - Functions, Part 1

Definition Of A Function

Page 16: An Introduction To Python - Functions, Part 1

Order Matters!

• Python is an interpreted language. This means that it needs to “see” a function before you call it…

myChoice = selectFlavor(0)

myChoice = selectFlavor(0)

Image Credit: www.dreamstime.com

Page 17: An Introduction To Python - Functions, Part 1

Order Definition Weirdness

• It turns out that you CAN call a function before it is defined – from another function.

def a() : c = b(4) return (c)

def b(d) : d =10return(d)

Image Credit: www.acclaimclipart.com

Page 18: An Introduction To Python - Functions, Part 1

Functions: Going All In

• It is possible to create a Python program that is all functions…

def main(): temp = a

def a() : c = b(4) return (c)

def b(d) : d =10return(d)

# start programmain()

Page 19: An Introduction To Python - Functions, Part 1

Revisiting The Worst Homework Assignment

EVER!• Using the 15 sets of patient data that you’ve

been given in Homework #2, sort each one of them in ascending order based on each of the 11 fields in a record…

• … using functions.

Image Credit: www.spiritofthesouth.net

Page 20: An Introduction To Python - Functions, Part 1

What’s In Your Python Toolbox?

print() math strings I/O IF/Else elif While For

Lists And / Or Functions

Page 21: An Introduction To Python - Functions, Part 1

What We Covered Today

1. Functions, Part 1

Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/

Page 22: An Introduction To Python - Functions, Part 1

What We’ll Be Covering Next Time

1. Functions, Part 2

Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/