an introduction to python - functions, part 1

Post on 17-Jul-2015

404 Views

Category:

Education

7 Downloads

Preview:

Click to see full reader

TRANSCRIPT

An Introduction To Software Development

Using Python

Spring Semester, 2014

Class #17: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

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

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

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

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

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

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

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.

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.

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

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

Send The Result Back

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

return selection

Image Credit: www.clipartpanda.com

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

Definition Of A Function

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

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

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()

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

What’s In Your Python Toolbox?

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

Lists And / Or Functions

What We Covered Today

1. Functions, Part 1

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

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/

top related