Functions
Calvin (Deutschbein)
W6Mon: 30 Sep
Announcements
- By tonight 11:59 PM: "Project 1: Wordle"
- My late policy is "no".
- Sunday PM: ~40% have submitted no work.
- We had work time in every class period last week!
- QUAD Center open 6 PM to 9 PM (and there are more of them than me).
- Midterm on Friday.
- Practice Exams posted
- The next two days should be helpful on particularly questions 2-4.
- There is a practice exam on Canvas where the exam will be hosted.
Function "Review"
- A function is a named code block.
- Prevents typos
- Organizes thinking
- Some reminders about vocabulary:
- Invoking or running a function by name is known as
calling that function.
- When calling a function, pass information to a function using
arguments or parameters.
- When a function completes its block of code, it
returns to its caller.
- The function gives information back to the caller by
returning a result
- About use:
- Functions cannot be used before they are defined.
- Code after a return is never runned (the function "ends" at return)
"Messy" or "Dangerous" returns
- If code works, it works.
- But clear thinking will serve you life/school etc.
- Work directly with the boolean values when possible
- Try not to code patterns like the following:
def is_divisible_by(x, y):
if x % y == 0: # just return here instead of 'if'
return True
else:
return False
- Generally speaking, we don't need to write out "True"
if is_divisible_by(i,7) == True: # remove '== True'
print(i)
Argument/Parameter Purposes
- Often functions should work in more than one case.
- E.g. "What is the longest no-repeat word in a certain language (not just English).
- Imagine you were trying to accomplish the task yourself!
- What is the minimum amount of information you would need to
know?
- There is always a balance
- More parameters makes your function more general, to be used
elsewhere
- More parameters are tedious and potentially error prone to enter if
unnecessary
Embiggen
- Functions can contain their own variables.
- Whenever Python enters a new function, variables used within that function cease to exist when the function returns/ends.
>>> x = 1
>>> def embiggen():
... x = 2
... x = x + 2
... return
...
>>> x
1
>>> embiggen()
>>> x
1
- "Embiggen's x" and "my x" are different x's, and things in "embiggen" do not affect "my x"
Taking a Walk through Vegas
def Vegas(x): # what happens in Vegas etc etc
y = 2
for i in range(3):
DEBUG and print("Vegas: i = ",i,"| x = ",x,"| y = ",y)
x += y
return x
- We can test it:
>>> DEBUG = True
>>> x = 3
>>> z = Vegas(x)
Vegas: i = 0 | x = 3 | y = 2
Vegas: i = 1 | x = 5 | y = 2
Vegas: i = 2 | x = 7 | y = 2
>>> print("Mine: x = ", x, "| z = ", z)
Mine: x = 3 | z = 9
Summary of a Function Call
- Evaluate the arguments in the context of the
caller
print(2 + 2) # same as print(4)
Summary of a Function Call
- Evaluate the arguments in the context of the
caller
print(2 + 2) # same as print(4)
- Copy argument to the corresponding
parameter variable
def double_print(str):
print(2 * str)
double_print("mur") # same as print(2 * "mur")
Summary of a Function Call
- Evaluate the arguments in the context of the
caller
print(2 + 2) # same as print(4)
- Copy argument to the corresponding
parameter variable
double_print("mur") # same as print(2 * "mur")
- Evaluate statements in the function body, using
the variables named within the function.
def print_n_times(x, str):
# n is 2, not 3, because "my n" and "double print n" differ
print(x * str)
n = 3
print_n_times(2, "mur") # same as print(2 * "mur")
Summary of a Function Call
- Evaluate the arguments in the context of the
caller
print(2 + 2) # same as print(4)
- Copy argument to the corresponding
parameter variable
double_print("mur") # same as print(2 * "mur")
- Evaluate statements in the function body, using
the variables named within the function.
n = 3
print_n_times(2, "mur") # same as print(2 * "mur")
- On 'return', compute the return value and
substitute that value in place of the function call
def repeat_n_times(x, str):
return x * str
s = repeat_n_times(2, "mur") # same as s = 2 * 'mur'
print(s) # prints "murmur"
Scope
- When Python encounters a variable name in a program, it looks for
where the variable was defined in an expanding search:
- Local - Parameters/arguments or other variables within some function. Like letters within "enter_action".
- Enclosing - Names inside a a calling function - like 'gw' within "enter_action"
- Global - Things outside of any function, perhaps imported, like 'ENGLISH_WORDS'
- Built-in - Things always defined everywhere, like "print"
etc.
- The part of a program in which a name is defined in called its
scope
- Always define a function or variable before you use it.
Scope
- Functions are just a special kind of variable.
def no_duplicates(word:str) -> bool:
for i in range(len(word)):
if word[i] in word[:i]:
return False
return True
def no_vowels(word:str) -> bool:
for letter in word:
if letter in "aeiou":
return False
return True
def longest_which_funcs(func) -> str:
candidate = ""
for word in ENGLISH_WORDS:
if len(word) > candidate and func(word):
candidate = word
return candidate
Scope
- Functions are just a special kind of variable.
>>> no_duplicates("aorta")
False
>>> no_vowels("why")
True
>>> longest_which_funcs(no_duplicates)
'ambidextrously'
>>> longest_which_funcs(no_vowels)
'glycyls'
Announcements
- By tonight 11:59 PM: "Project 1: Wordle"
- Sunday PM: ~40% have submitted no work.
- We had work time in every class period last week!
- QUAD Center open 6 PM to 9 PM (and there are more of them than me).
- Midterm on Friday.
- Practice Exams posted.
- The next two days should be helpful on particularly questions 2-4.
- There is a practice exam on Canvas where the exam will be hosted.
- More on Wednesday.