cs 105
play

CS 105 Lecture 4: Functions Craig Zilles (Computer Science) - PowerPoint PPT Presentation

CS 105 Lecture 4: Functions Craig Zilles (Computer Science) https://go.illinois.edu/cs105fa19 February 16, 2020 Are you sitti ting next t to someone to talk to for th the clicker questi tions? To Today I'm using: pythontutor.com 2 Bi


  1. CS 105 Lecture 4: Functions Craig Zilles (Computer Science) https://go.illinois.edu/cs105fa19 February 16, 2020

  2. Are you sitti ting next t to someone to talk to for th the clicker questi tions? To Today I'm using: pythontutor.com 2

  3. Bi Big Pictu cture (Muddiest t Poi oints ts) • if the challenge questions are too hard can we leave some of it? • In general, I'd say that there is no single concept that confuses me most as of right now. I kind of understand why I need to do what I need to do in coding, but I am still foggy as to where each and every little character goes, and what makes something a syntax error and what doesn't. 3

  4. To Today 1. Warmup • Negative indices 2. Functions • Indentation / Code Blocks • Parameters and Arguments • Return Values & None 3. Functions in Excel 4. Next week's reading: Booleans & Conditionals 4

  5. Neg Negative e Indexi xing "abcde"[-2] What is the value of the above expression? A) 'a' B) 'b' C) 'c' D) 'd' E) 'e' 5

  6. Se Sets ts • When to use: • You have a collection of similar things • You want to know if something is in the set or not • You want to do set operations (e.g., intersection) • Finding birthdays shared by two or more people in the class 6

  7. Di Dictionaries es • A mapping type : given a piece of info, find related info • Key-Value pairs • Keys must be unique , values can be repeated • What is it used for? • UIN -> student record • Cell phone number -> cell tower (service provider) 7

  8. Wh What type of data collection is this? {"Craig", "Anant", "Sofia", "Chinny"} A) Dictionary B) List C) Set D) String E) Tuple 8

  9. Us User-de define ned d Func unctions ns using recipes in recipes 9

  10. Us User-de define ned d Func unctions ns • Sequences of operations for use elsewhere in program • Function definition says what to do def <name> (): <body> • Function calls/invocations actually run the function <name>() 10

  11. Us User-de define ned d Func unctions ns • Sequences of operations for use elsewhere in program • Function definition says what to do def get_input_and_print(): name = input("Your name?\n") print("Hello " + name + "!") • Function calls/invocations actually run the function get_input_and_print() 11

  12. Re Representative Muddiest Points • The sections on parameters and returns made no sense to me. I don't understand when to indent something, or what to return and when. • I feel like I do not understand what code block and indentations stand for in Python. 12

  13. Cod Code Bl Block ocks • Need a way to tell Python "this statements are related" • Python uses indentation Code Block A Control flow construct: Code Block B (Execution determined by control flow construct) Code Block C (Same indentation as A) 13

  14. Indentation In tion • In other prog. languages, indentation is just good style • In Python, it is syntactic and semantic • These three programs are all different • Text is same, white space and behavior is different def test(): def test(): def test(): print('first') print('first') print('first') print('second') print('second') print('second') test() test() test() 14

  15. Wh What does this program output? def test(): print('first') print('second') • A) it raises an error test() • B) first • C) first second • D) first second • E) second first 15

  16. An Annou ounce cements ts • We have reading assignments due every Saturday • Please don't make this a source of drama • Muddiest Points should be about most recent reading • Tutoring available! cs105-tutor@illinois.edu • Lab this week: Debugging! (and intro. to Colab) • Quiz 1 this week (Thursday - Saturday ) • Taken at home on PrairieLearn • To be done Alone! • Practice exam up soon (but do HW#4 first anyway) 16

  17. Ho How much to total time di did y d you spe u spend i nd in n th the past t week on CS 105? • Lecture + Lab = 3 hours • Readings + Preflights + HW + Office hours + Exam0 • Which of these expressions are True for you A) hours_spent < 6 hours B) 6 hours <= hours_spent < 9 hours C) 9 hours <= hours_spent < 11 hours D) 11 hours <= hours_spent < 13 hours E) hours_spent >= 13 hours 17

  18. Pa Parameters def welcome_message(first, last): # function body here … welcome_message("Harry", "Potter") • Passing parameters is just like an assignment statement • Binds new name to an existing value within the function • Code inside the function can access value using name • Name disappears when function is over 18

  19. Wh What value is printed? def do_thing(v1, v2, v3): a = v2 b = v1 + 1 print(a * b) do_thing(3, 2, 0) A) 0 B) 6 C) 8 D) 9 E) any other number 19

  20. Wh What value is printed? def do_thing(var1): var1 = 2 var1 = 1 do_thing(var1) print(var1) A) 0 B) 1 C) 2 D) any other number E) Error occurs 20

  21. Wh What value is printed? def do_thing(var1): var1.append(4) var1 = [1, 2, 3] do_thing(var1) print(len(var1)) A) 0 B) 3 C) 4 D) any other number E) Error occurs 21

  22. Re Return Values • If we need a function to produce a value, return will exit the function, and replace the function call with the returned value • Function calls can be part of arbitrary expressions • If there is no return, the function terminates when it reaches the bottom (and returns None) 22

  23. Wh Which assigns x to 5? A) x = f1() def f1(): return 5 B) x = f2() def f2(): C) x = f3() print(5) D) All of the above def f3(): return print(5) E) None of the above 23

  24. Paramet eters, Arguments, Ret eturn Values def welcome_message(first, last): message = "Welcome " + first + " " + last message += " to CS 105!" return message msg = welcome_message("Harry", "Potter") 24

  25. Fu Function Composi sition def f(x): return 3 * x What value is returned by f(f(2)) ? A) 3 B) 6 C) 9 D) 12 E) 18 25

  26. No None • I don't understand the value of None, and what it means when you don't have the return statement. • Would there ever be a time that we would need a function to return the value of "none"? • Mostly this is important to know because you might do something like this by accident: x = print("hi there!") 26

  27. Fu Functions s vs. s. Methods • Methods are functions that are part of an object/type • They use dot notation • For example: my_list = [0, 1, 2, 3] my_list.append(22) • Functions, in contrast: len(my_list) 27

  28. Fu Functions s in Excel el • Excel provides many useful "built-in" functions: • E.g., SUM(), AVERAGE(), MIN() • Take arguments: cells, cell ranges • Produce return values • Can be part of expressions & assignments 28

  29. Wh What bugs are in the following code? def add_one(x): return x + 1 x = 2 x = x + add_one(x) A) No bugs. The code is fine. B) The function body is not indented. C) We use x as both a parameter and a variable, but we are not allowed to do that D) B and C 29

  30. Ne Next week eek's s rea eading • Sometimes we want to "conditionally" execute code • Send email to a student only if they missed Exam 0 • Booleans: a variable type that is either True or False • Relational statements: Boolean from comparing 2 things • x < 7 • Boolean operators: using multiple relational statements • x < 7 and z > 2 • Conditional blocks: execute code only if condition is true if x < 7: print('hi') 30

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend