Top Banner
Func%ons How can we use func,ons to break up a program into chunks that are easy to change and understand?
21

Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Jun 07, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Func%ons  

How  can  we  use  func,ons  to  break  up  a  program  into  chunks  that  are  easy  to  change  and  understand?  

Page 2: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Warmup  

•  What  is  be3er,  a  house  with  one  giant  room,  or  a  house  with  many  different  rooms?    Why?  

Page 3: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Why  func%ons  

•  Programs  get  big  and  complicated  – break  them  up  into  smaller  pieces  – easier  to  write  and  fix  

•  A  func%on  is  a  chunk  of  code  that  does  something  (a  specific  task)  

Page 4: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Wri%ng  func%ons  

•  Start  with  the  keyword  def  (for  define)  •  Name  the  func%on  •  Then  put  parentheses  ()  •  Then  put  a  colon  :  •  Example  

•  Indent  all  the  code  in  that  func%on  

Page 5: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Using  func%ons  

•  Func%ons  don’t  run  un%l  you  “call”  them  – Use  the  name  of  the  func%on  followed  by  ()  – Example  – Where  have  we  seen  this  before?  

Page 6: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Where  have  we  seen  func%ons  before?  

•  Codingbat  – What  we  wrote  on  codingbat  were  func%ons,  to  be  used  by  other  programs  

– How  did  we  produce  results  from  these  func%ons?  

•  Code.org  – What  are  the  func%ons?  – What  are  the  func%on  calls?  

Page 7: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

10.1_myaddress.py  

•  Make  a  func%on  called  printmyaddress()  which  will  print  your  proper  address  on  three  lines.    Then  call  the  func%on.    Follow  the  example  

Page 8: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

10.2_ini%als.py  

•  Write  a  func%on  to  print  your  ini%als  in  big  le3ers,  like  this:  

•  Then  call  the  func%on  

Page 9: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a
Page 10: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Func%on  Arguments  

•  Pieces  of  informa%on  you  “pass”  to  a  func%on  when  you  call  it  

•  Go  inside  the  parentheses  of  the  call  

•  Func%on  has  to  “receive”  the  arguments  •  A  variable  goes  inside  the  parentheses  of  the  func%on  define  line.  

Page 11: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Mul%ple  arguments  

•  Can  have  many  arguments  in  a  func%on,  separate  them  by  commas  

Page 12: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Func%on  Arguments  

•  Without  argument  

•  With  argument:  more  flexibility!  

Page 13: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Func%ons  and  programs  

•  Func%ons  are  separate  from  programs  – “Black  box”  – Programs  don’t  know  what’s  happening  in  func%ons  at  all  

Page 14: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Returning  a  Value  

Page 15: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Returning  a  value  

•  Func%ons  can  return  a  value  to  the  program  –  In  the  func%on  have  a  return  line  at  the  end  – This  value  can  be  printed,  saved  to  a  variable,  etc.  

–  In  the  example,  the  func%on  calculates  the  price  with  tax,  puts  it  in  the  variable  taxtotal,  and  then  returns  taxtotal  to  the  program  that  called  it  

Page 16: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

Local  vs.  Global  variables  

•  Variables  in  func%ons  are  local  and  exist  only  while  the  func%on  is  being  run  

–  In  this  example,  price,  tax_rate,  and  taxTotal  are  local  variables  

•  Variables  in  programs  are  global  and  stay  in  memory  throughout  the  program  – Global  variables  can  be  used  but  NOT  changed  in  a  func%on  

Page 17: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

10.3_calculatetax.py  

•  Write  the  following  func%on  

•  How  can  the  func%on  be  called?  – Print  it  

–  -­‐  it  to  a  variable  

Page 18: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

10.3_calculatetax.py  

•  User  input  

Page 19: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

10.4_addtwo.py  •  This  program  sums  2  numbers  and  prints  them  

•  Instead  of  finding  the  sum  in  the  program,  write  a  func%on  to  find  the  sum  – Pass  the  two  numbers  num1  and  num2  to  the  func%on  – The  func%on  will  add  the  two  numbers  – The  func%on  will  return  the  sum  – The  program  will  call  the  func%on,  assign  it  to  the  variable  numSum  

Page 20: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

10.5_anyaddress.py  

•  Make  a  func%on  that  will  print  an  address  with  user  input  –   name,  address,  street,  city,  state,  or  zip  in  the  USA.    

•  (It  needs  6  arguments-­‐  collect  and  pass  them  individually  or  as  a  list)  

Page 21: Funcons - serviceandscience.weebly.com › uploads › 3 › 7 › 7 › ... · 10.4_addtwo.py(• This(program(sums(2(numbers(and(prints(them(• Instead(of(finding(the(sum(in(the(program,(write(a

10.6_change.py  

•  Write  a  func%on  to  calculate  the  total  value  of  some  change—quarters,  dimes,  nickels,  and  pennies  .  The  func%on  should  return  the  total  value  of  the  coins.  Then  write  a  program  that  calls  the  func%on.  The  output  should  look  like  this  when  it  runs: