Functions
Returning a value
Piecemeal solutions (using functions with return values)
The “overall procedure”:
You walk into a food court at the mall and want to create your own fancy sandwich, consisting of a bun from vendor 1, the protein from vendor 2, and the spread from vendor 3.
So, you ask vendor 1 for the bun, take it to vendor 2 and ask them to put the protein on it (and return “the creation” back to you), then you take it to vendor 3 and ask them to put the spread on, and return it back for your consumption.
def get_bun():
# do whatever...
return a_tasty_bun
def put_on_protein(some_sort_of_bun):
…
fulfilled_sandwitch = complex_process(some_sort_of_bun)
return fulfilled_sandwitch
…
# main:
bun = get_bun()
sandwich = put_on_protein(bun)
sandwich = put_on_spread(sandwich)
consume(sandwich)
#----------------------------------------------------------
Or all together (unpack it like an onion :): consume(put_on_spread(put_on_protein(get_bun())))
# OR, main:
sandwich = put_on_protein(get_bun())
sandwich = put_on_spread(sandwich)
consume(sandwich)
# OR, main:
bun = get_bun()
Sandwich = put_on_spread(put_on_protein(bun))
consume(sandwich)
(uncover this)
Functions which return a value enable “chaining”
(uncover this) The ultimate “chaining”
Functions Returning Values - go over this and predict/write down what happens
input(“your prompt/question here”) # returns a value, but
age = input(“How old are you?”) # you need to assign the returned value to a variable
-------------------------------------------------
range(start, end, step) # returns a value, but you need to do something with it
range(1, 9, 2) # what is produced?
print range(2, 9, 3) # you need to do something (in this case, print the value)
for i in range(1, 3): # you can assign the returned value to a variable
print i * i # what is produced?
-------------------------------------------------
round(number, places) # returns a value
round(1/9, 3) # what is produced?
cost = round(100 / 3, 2) # you need to assign the returned value to a variable
print round(cost, 1) # you need to do something with the returned value
print cost # what is produced?
cost = round(cost, 1)
print cost # what is produced?
Some functions DO NOT return anything
print “Hi there!”
---------------------
toad.goto(0, 0)
---------------------
def f1(a):
sqr = a * a
print sqr
--------------------------------
Some functions DO return something
def f1(a):
sqr = a * a
return sqr
Functions with Parameters
Example:
def any_digits(word):
for c in word:
if c in "0123456789":
return True
return False
name = input("What's your favorite movie star?")
if any_digits(name):
print "you must love Star Wars!"
else:
print "Wow, me too!"
Exercise 1:
Study the example on the left, first.
Write a function (you can call it two_negatives(...) ) which gets 3 numbers as parameters and returns True if there are at least 2 negative numbers, and False otherwise.
In your main program ask the user for 3 numbers then use this function and print an appropriate message.
Functions with Parameters
def count_a_in_word(word):
count = 0
for c in word:
if c == "a":
count = count + 1
return count
# The main program:
name = input("give me a word")
number_of_a = count_a_in_word(name)
if number_of_a > 0:
print "I found", number_of_a, "a's in your word"
else:
print "sorry, no a's in your word"
Exercises 2, 3:
2. Study and modify the function on the left (call it count_vowels_in_word(...) ) to count the number of occurrences of vowels in word (not just ‘a’s).
Modify the code so that it prints a grammatically correct message for an occurrence of 1 vowel, and more than 1.
3. Create another function count_total_occurrances(...), which takes a word and a string of 3 characters, and returns the combined number of times (i.e., the sum of) the 3 characters appeared in the word.
For example if the word is
“abracadabra” and the 3 character
string is “acd”, then your function
returns 7
(since ‘a’ shows up 5 times + ‘c’ shows up 1 + ‘d’ shows up 1)
Lab Activity
Primeness project