1 of 17

Newton Girls Who Code

Tuesday Session 22, 2018-03-27

2 of 17

Announcements

  • 4/10: guest speaker Bela Labovitch, Athena Health
  • 4/25: field trip to Athena Health

�Watch for field trip sign-up email.

3 of 17

Program Design Activity

  • Form groups of 2-3 people
  • Get pens and paper
  • Lids down, screens off�

4 of 17

Today

  • Tutorial on Functions
  • How much Sun-screen?
  • Free code
  • Chill time�

Lids down, screens off�

5 of 17

A Function

def draw_square():

i = 0

while i < 4:

forward(10)

turn_right()

i = i + 1

draw_square()

5

3.01

MA1502

BHS ‘18

6 of 17

A Function with Arguments

def draw_square(side_length):

i = 0

while i < 4:

forward(side_length)

turn_right()

i = i + 1

draw_square(10)

draw_sqaure(20)

6

3.01

MA1502

BHS ‘18

7 of 17

A Fruitful Function

def fahrenheit_to_celsius(f_temp):

c_temp = 5/9 * (f_temp - 32)

return c_temp

fahrenheit = int(input("What temperature F is it today?"))

celsius = fahrenheit_to_celsius(fahrenheit)

print(str(fahrenheit) + "F is " + str(celsius) + "C")

7

3.02

MA1502

BHS ‘18

8 of 17

Why Functions?

  1. Name a group of statements, which makes your program easier to read and debug.
  2. Eliminating repetitive code. Later, if you make a change, you only have to make it in one place.
  3. Dividing a long program into functions allows you to debug the parts one at a time and then assemble them into a working whole.
  4. Well-designed functions are often useful for many programs. Once you write and debug one, you can reuse it.

8

3.01

MA1502

BHS ‘18

9 of 17

Flow of Execution

print("Hello, welcome to my program")

a = 3

b = 7

print("a: " + str(a) + " b: " + str(b))

print("a + b = " + str(a+b))

response = input("What should I change 'b' to?")

b = int(response)

print("Now a: " + str(a) + " b: " + str(b))

print("a + b = " + str(a+b))

9

3.02

MA1502

BHS ‘18

10 of 17

Flow of Execution

response = input("What temperature F is it today?")

fahrenheit = int(response)

celsius = 5/9 * (fahrenheit - 32)

print(str(fahrenheit) + "F is " + str(celsius) + "C")

10

3.02

MA1502

BHS ‘18

11 of 17

Flow of Execution

def fahrenheit_to_celsius(f_temp):

c_temp = 5/9 * (f_temp - 32)

return c_temp

fahrenheit = int(input("What temperature F is it today?"))

celsius = fahrenheit_to_celsius(fahrenheit)

print(str(fahrenheit) + "F is " + str(celsius) + "C")

11

3.02

MA1502

BHS ‘18

12 of 17

Flow of Execution

def fahrenheit_to_celsius(f_temp):

c_temp = 5/9 * (f_temp - 32)

print("{0}F is {1:.1f}C".format(f_temp, c_temp))

fahrenheit_to_celsius(32)

fahrenheit_to_celsius(80)

response = input("What temperature F is it today?")

fahrenheit_to_celsius(int(response))

12

3.02

MA1502

BHS ‘18

13 of 17

Returning Vs. Printing

  • Return = “give” a value back to the “caller”

13

Function B

Function A

Call

Return

3.01

MA1502

BHS ‘18

14 of 17

Returning Vs. Printing

  • Return = “give” a value back to the “caller”
  • Print = show something on the screen

14

Function B

Function A

Call

Print

3.01

MA1502

BHS ‘18

15 of 17

Example

def fahrenheit_to_celsius(f_temp):

c_temp = 5/9 * (f_temp - 32)

return str(f_temp) + "F is " + str(c_temp) + "C")

print(fahrenheit_to_celsius(32))

print(fahrenheit_to_celsius(80))

response = input("What temperature F is it today?")

print(fahrenheit_to_celsius(int(response)))

15

3.03

MA1502

BHS ‘18

16 of 17

Chill Time

Have fun! Use this time to:

  • Hang out with your friends
  • Share a joke with the club
  • Geek out about hobbies, books,

bands, comics, videos, etc.

  • Play video games
  • Write in a journal
  • Hack more code
  • Show off your side projects

17 of 17

See you next week!

Don’t forget to be awesome!