Lecture 4: Functions
Craig Zilles (Computer Science) February 16, 2020 https://go.illinois.edu/cs105fa19
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
Craig Zilles (Computer Science) February 16, 2020 https://go.illinois.edu/cs105fa19
2
some of it?
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
What is the value of the above expression? A) 'a' B) 'b' C) 'c' D) 'd' E) 'e'
5
class
6
given a piece of info, find related info
7
{"Craig", "Anant", "Sofia", "Chinny"} A) Dictionary B) List C) Set D) String E) Tuple
8
using recipes in recipes
9
def <name>(): <body>
<name>()
10
def get_input_and_print(): name = input("Your name?\n") print("Hello " + name + "!")
get_input_and_print()
11
to me. I don't understand when to indent something, or what to return and when.
indentations stand for in Python.
12
13
Code Block A Code Block B (Execution determined by control flow construct) Control flow construct: Code Block C (Same indentation as A)
14
def test(): print('first') print('second') test() def test(): print('first') print('second') test() def test(): print('first') print('second') test()
15
first first second second first first second
def test(): print('first') print('second') test()
16
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
function
18
def welcome_message(first, last): # function body here … welcome_message("Harry", "Potter")
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
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
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
exit the function, and replace the function call with the returned value
reaches the bottom (and returns None)
22
def f1(): return 5 def f2(): print(5) def f3(): return print(5)
23
A) x = f1() B) x = f2() C) x = f3() D) All of the above E) None of the above
def welcome_message(first, last): message = "Welcome " + first + " " + last message += " to CS 105!" return message msg = welcome_message("Harry", "Potter")
24
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
when you don't have the return statement.
function to return the value of "none"?
something like this by accident: x = print("hi there!")
26
my_list = [0, 1, 2, 3] my_list.append(22)
len(my_list)
27
28
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
if x < 7: print('hi')
30