Intro to
Computer
Science

Spring 2021
course
site
-->

Mon March 22

... 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

function concept

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

example

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.

practice

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?

practice 2

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".

practice 3

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.

practice with arguments and return values ... if we get this far.

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".

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.

https://cs.bennington.college /courses /spring2021 /introcs /notes /march22
last modified Mon March 22 2021 11:30 am

attachments [paper clip]

  last modified size
TXT double.py Mon Mar 22 2021 11:30 am 292B
TXT double_in_place.py Mon Mar 22 2021 11:30 am 407B
TXT double_list.py Mon Mar 22 2021 11:30 am 438B
TXT sum.py Mon Mar 22 2021 11:30 am 577B
TXT try_changing_number.py Mon Mar 22 2021 11:30 am 673B