... start recording.
Questions about anything?
Please read chapter 6 in the textbook, on functions. We'll discuss and practice this material today. Coding exercises are due Thursday.
where we're going:
* next week : decisions with "if" statements
... last "all you really need" idea
* after that: midterm group projects
* 2nd half of term : practice, more containers, your project
Data is stored in variables - those are like nouns.
Functions are the like "verbs" that we "invoke" to do things.
The function definition is a recipe that says what to do.
And we pass information into the function with "arguments", and out of the function with a "return".
vocabulary
argument x in `sqrt(x)` ; what goes in
return value y in `y = sqrt(x)` ; what comes out
local variable variables within functions
side effect anything a function does other than return
invoke run the function, for example `print(sqrt(5))`
Let's look at this example in python tutor, and walk through it carefully.
import math
smallest = 1
largest = 10
def print_root(x):
""" print x and its square root """
root = math.sqrt(x)
print(f" {x : 12d} {root : 12.3f} ")
def main():
print("square roots")
for number in range(smallest, largest+1):
print_root(number)
main()
Then I'll use Zelle's chapter 6 slides and my notes on functions to show you these ideas.
Supposed I want to write a program that does this :
$ python dots.py
How many dots? 8
What is the word? hello
. . . . . . . . hello . . . . . . . .
First, can we do this without any functions at all? Let's remember how to do this sort of thing in python ... google "python repeat symbol"
Second, can we (if we try) break the task into smaller function pieces? Let's take the "repeat n times" and put that into a function. Can we we also take the part that makes (before_hello, hello, after_hello) into a function?
What should be the inputs? The return value?
Modify the acronym program to include
a function acronym(words)
that returns
the acronym from a given string of words.
We'll do this together.
Also ... "doc strings".
Now its your turn - breakout rooms. Try this one.
Write a definition for a function `sum_1_to_n(n)`
which returns the sum of the integers 1 to n.
Use this in a `main()` function which prompts
for n and then prints their sum.
Here are some exercises to practice in class ... which get at some of the trickier aspects of all this, in particular the difference between a "return value" "modify in place".
double
which doubles a number and returns it. Use it in a program that asks for a number, then prints out the double double (in other words, four times) of a number.Can you pass in a number and modify it in place? (Answer: no. Try to write code that does this, and see what happens.)
All the files that I showed you in class are attached below, including the one that asked you to do as a breakout room exercise.
last modified | size | ||
double.py | Mon Mar 22 2021 11:30 am | 292B | |
double_in_place.py | Mon Mar 22 2021 11:30 am | 407B | |
double_list.py | Mon Mar 22 2021 11:30 am | 438B | |
sum.py | Mon Mar 22 2021 11:30 am | 577B | |
try_changing_number.py | Mon Mar 22 2021 11:30 am | 673B |