1 of 102

Discussion 2:

Functions + Control

Course Website: cs10.org

�Grab a worksheet!

We’ll start at 4:12

Begin the group activity on the worksheet!

2 of 102

Question of the Day

Ask the person to your right / left about their opinions on the consumption of caffeine. Do you have it? If so, how much? Do you have coffee, tea, energy drinks, etc.?

  • The average cup of coffee has 95mg of caffeine.
  • The effects of 95mg of caffeine can last between 4 to 6 hours
  • Caffeine can affect sleep too, by reducing the:
    • Total sleep time
    • Deep sleep amount of time
    • Sleep efficiency

3 of 102

Agenda

  • iClicker
  • Join in small groups
  • Work on Section I
    • Go over Questions in Big Group
  • Work on Section II
    • Go over Questions in Big Group
  • Work on Section III
    • Go over Questions in Big Group
  • Q&A + Feedback Form

4 of 102

Logging into iClicker

  • 4-5PM Discussion (Soda 438)
    • Enroll into join.iclicker.com/ZNLT

  • 5-6PM Discussion (Stanley 106)
    • Enroll into join.iclicker.com/YRIL

5 of 102

Questions + Comments from Feedback Form

  • What do I do if I miss lab
  • whats some cool ideas for custom blocks one can create?

6 of 102

Worksheet Time!

7 of 102

Section I - Fucntions

8 of 102

Section I - Functions

  1. What will the following return? Why?

9 of 102

Section I - Functions

  • What will the following return? Why?

False, because the data types are different. The left “true” is a boolean data type and the right is text.

10 of 102

Section I - Functions

  • What will the following return? Why?

11 of 102

Defining Data Types, Domain, Range

  • What’s a data-type? Why is it useful to know the data-type of a variable?
  • What does domain mean in the context of data-types? How do we figure out the domain of a Snap! block?
  • What does range mean in the context of data-types? How do we figure out the range of a Snap! Block?

12 of 102

Question 2

  • What’s a data-type, and why is it useful to know?
    • A restriction / classification on a value
    • Can be helpful if we only want certain types of data!
  • What does domain mean in the context of data-types? How do we figure out the domain of a Snap! Block?
    • Domain is the data type of the input / parameter passed in
    • We can figure it out by looking at the parameter shape
  • What does range mean in the context of data-types?
    • Range is the is the data type of the output / returned value
    • We can figure it out by looking at the shape of the block

13 of 102

Restricting Data Types in Snap!

Number

Text (string)

Boolean

List

Function

Can be anything! No restrictions

14 of 102

Q3: What are the data types, domain, range of each function:

Block

Type

Domain

Range

15 of 102

What are the data types, domain, range of each function:

Block

Type

Domain

Range

command

number

none

reporter

numbers

number

predicate

anything

boolean

predicate

List, anything

boolean

16 of 102

Q4: What are the data types of each function:

Function Name

Domain

Range

foo

sqrt

function

17 of 102

Q4: What are the data types of each function:

Function Name

Domain

Range

foo

boolean

number

sqrt

number

number

function

number

anything

18 of 102

Section II - Variables

19 of 102

Guess that Scope!

20 of 102

Guess that Scope!

Reports: 10

21 of 102

Guess that Scope!

22 of 102

Guess that Scope!

Reports: 10

23 of 102

Guess that Scope!

24 of 102

Guess that Scope!

Reports: 20

a = 10, b = 10

25 of 102

Guess that Scope!

26 of 102

Guess that Scope!

Reports: 10

a = 20, b = 10

27 of 102

Section III - Control Structures

28 of 102

Boolean + Truth Tables

29 of 102

Boolean + Truth Tables

30 of 102

Boolean + Truth Tables

True

True

False

False

True

True

False

False

True

True

False

False

True

True

False

False

31 of 102

Boolean + Truth Tables

True

True

False

False

True

True

False

False

True

True

False

False

True

True

False

False

True and True

True and False

False and True

False and False

True or True

True or False

False or True

False or False

32 of 102

Boolean + Truth Tables

True

True

False

False

True

True

False

False

True

True

False

False

True

True

False

False

True

False

False

False

True

True

True

False

33 of 102

Boolean + Truth Tables

34 of 102

Boolean + Truth Tables

a

b

(not (a or b) = false)

True

True

True

False

False

True

False

False

35 of 102

Boolean + Truth Tables

a

b

(a or b)

Not (a or b)

= false

Result

True

True

True

False

False

True

False

False

36 of 102

Boolean + Truth Tables

a

b

(a or b)

Not (a or b)

= false

Result

True

True

(T or T)

True

False

(T or F)

False

True

(F or T)

False

False

(F or F)

37 of 102

Boolean + Truth Tables

a

b

(a or b)

Not (a or b)

= false

Result

True

True

(T or T)

Not ( )

True

False

(T or F)

Not ()

False

True

(F or T)

Not ()

False

False

(F or F)

Not ()

38 of 102

Boolean + Truth Tables

a

b

(a or b)

Not (a or b)

= false

Result

True

True

(T or T)

Not ( )

= F

True

False

(T or F)

Not ( )

= F

False

True

(F or T)

Not ( )

= F

False

False

(F or F)

Not ( )

= F

39 of 102

Boolean + Truth Tables

a

b

(a or b)

Not (a or b)

= false

Result

True

True

(T or T)

Not (T)

F

F=F

T

True

False

(T or F)

Not (T)

F

F=F

T

False

True

(F or T)

Not (T)

F

F=F

T

False

False

(F or F)

Not (F)

T

T=F

F

40 of 102

Boolean + Truth Tables

a

b

(not (a or b) = false)

True

True

True

True

False

True

False

True

True

False

False

False

41 of 102

What will the following return?

42 of 102

What will the following return?

T

T

T

T

T

T

T

T

T

43 of 102

What will the following return?

T

T

T

44 of 102

What will the following return?

T

T

F

Because this is False the line doesn’t get run

45 of 102

What will the following return?

T

T

F

T

Evaluate

46 of 102

What will the following return?

T

T

F

F

Because this is False the line doesn’t get run

47 of 102

What will the following return?

T

T

F

T

T

Because this is True, we evaluate

F

48 of 102

What will the following return?

T

T

F

T

T

T

T

Not (T and T)

F

49 of 102

What will the following return?

T

T

F

T

T

T

Not (T and T)

F

50 of 102

What will the following return?

T

T

F

T

T

T

Not (T)

F

51 of 102

What will the following return?

T

T

F

T

T

T

F → so mystery(True, True) returns False

F

F

52 of 102

Will this ever return true?

53 of 102

Conditionals: Can this ever return True?

Determine with Option 1: Draw it out / Walk through it with the logic of the code

54 of 102

Conditionals: Can this ever return True?

  • If ‘a’ is T, not a, will be false

  • This statement is only evaluated then if ‘a’ is F → (not F) → T

55 of 102

Conditionals: Can this ever return True?

  • If ‘a’ is T, not a, will be false

  • This statement is only evaluated then if ‘a’ is F → (not F) → T

  • So, if ‘a’ is F, then what should ‘b’ have to make (‘a’ and ‘b’) return T?

56 of 102

Conditionals: Can this ever return True?

  • If ‘a’ is T, not a, will be false

  • This statement is only evaluated then if ‘a’ is F → (not F) → T

  • So, if ‘a’ is F, then what should ‘b’ have to make (‘a’ and ‘b’) return T?

  • It can never return T!

57 of 102

Conditionals: Can this ever return True?

  • For ‘b’ = F to be true, we need ‘b’ to be set to F (because F = F is True)

58 of 102

Conditionals: Can this ever return True?

  • What value of ‘b’ would make this statement true?

  • Hint: Look at your truth table!

59 of 102

Conditionals: Can this ever return True?

  • For ‘b’ = F to be true, we need ‘b’ to be set to F (because F = F is True)

60 of 102

Conditionals: Can this ever return True?

  • For ‘b’ = F to be true, we need ‘b’ to be set to F (because F = F is True)

  • What value of ‘a’ would make ((not a) = T) return True?

61 of 102

Conditionals: Can this ever return True?

  • For ‘b’ = F to be true, we need ‘b’ to be set to F (because F = F is True)

  • What value of ‘a’ would make ((not a) = T) return True?

  • If ‘a’ is F

62 of 102

Conditionals: Can this ever return True?

  • For ‘b’ = F to be true, we need ‘b’ to be set to F (because F = F is True)

  • What value of ‘a’ would make ((not a) = T) return True?

  • If ‘a’ is F

  • But wait, didn’t we already try that here?

63 of 102

Conditionals: Can this ever return True?

  • But wait, didn’t we already try that here?

  • We did! And, when ‘a’ was False, the statement was evaluated and the function returned False

64 of 102

Conditionals: Can this ever return True?

  • This if statement gets run before the else if, even though both statements return True

  • We already established that the first if statement always returns True.

  • The else if statement, even if it could technically return, True, will never get evaluated

65 of 102

Conditionals: Can this ever return True?

  • So, this else if statement either never gets evaluated because the statement evaluates to False or the function terminated from the previous statement

  • Or, the statement does get evaluated and then the report

< (not a) = true >

will always return False

66 of 102

Conditionals: Can this ever return True?

  • Else if <True> (with the green check mark) is the same as an else statement.

  • Meaning, the statement will always be evaluated if the above statements were not evaluated

  • For what values of ‘a’ and ‘b’ would the expression return True

67 of 102

Conditionals: Can this ever return True?

  • For what values of ‘a’ and ‘b’ would the expression return True: There’s a few!

  • But, we have the same problem as earlier.

68 of 102

Conditionals: Can this ever return True?

  • So the function never returns True!

69 of 102

Conditionals: Can this ever return True?

Determine with Option 2: Try all the True and False values for a and b, and see what gets outputted

70 of 102

Conditionals: Can this ever return True?

Determine with Option 3: Determine based on the truth table (but I find this way a little hard to read personally)

71 of 102

Truth Table

72 of 102

Conditionals

T

T

T

F

F

T

F

F

73 of 102

Conditionals

T

T

Not T

T

F

Not T

F

T

Not F

F

F

Not F

74 of 102

Conditionals

T

T

F

T

F

F

F

T

T

F

F

T

75 of 102

Conditionals

T

T

F

T and T

T

F

F

T and F

F

T

T

F and T

F

F

T

F and F

76 of 102

Conditionals

T

T

F

T

T

F

F

F

F

T

T

F

F

F

T

F

77 of 102

Conditionals

T

T

F

T

T = F

T

F

F

F

F = F

F

T

T

F

T = F

F

F

T

F

F = F

78 of 102

Conditionals

T

T

F

T

F

T

F

F

F

T

F

T

T

F

F

F

F

T

F

T

79 of 102

Conditionals

T

T

F

T

F

(Not T) = T

T

F

F

F

T

(Not T) = T

F

T

T

F

F

(Not F) = T

F

F

T

F

T

(Not F) = T

80 of 102

Conditionals

T

T

F

T

F

F = T

T

F

F

F

T

F = T

F

T

T

F

F

T = T

F

F

T

F

T

T = T

81 of 102

Conditionals

T

T

F

T

F

F

T

F

F

F

T

F

F

T

T

F

F

T

F

F

T

F

T

T

82 of 102

Conditionals

T

T

F

T

F

F

T

T

F

F

F

T

F

T

F

T

T

F

F

T

T

F

F

T

F

T

T

T

83 of 102

Conditionals

T

T

F

T

F

F

T

Not(T and T)

T

F

F

F

T

F

T

Not(T and F)

F

T

T

F

F

T

T

Not(F and T)

F

F

T

F

T

T

T

Not(F and F)

84 of 102

Conditionals

T

T

F

T

F

F

T

Not(T)

T

F

F

F

T

F

T

Not(F)

F

T

T

F

F

T

T

Not(F)

F

F

T

F

T

T

T

Not(F)

85 of 102

Conditionals

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

86 of 102

Conditionals: Blue = Statements, Red = Return Values

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

87 of 102

Conditionals: Yellow = Currently looking at values

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

  • Won’t be evaluated, since not A is F

88 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

  • Won’t be evaluated, since not A is F

89 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

  • Don’t care about these values since they return F

90 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

  • Don’t care about these values since they return F

91 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

  • Don’t care about these values since they return F

92 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

  • Won’t be evaluated because b = False is False when b is False

93 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

  • This one is the trickiest.

94 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

This is T and returns T, but the previous if statement was also T and returned F

95 of 102

Conditionals: Can this ever return True?

  • This if statement gets run before the else if, even though both statements return True

  • We already established that the first if statement always returns True.

  • The else if statement, even if it could technically return, True, will never get evaluated

96 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

This is T and returns T, but the previous if statement was also T and returned F

97 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

  • Returns False, so we can get rid of it

98 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

Similarly, True statements were already evaluated and returned False

99 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

So these are never evaluated either

100 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

  • Returns False, so we can get rid of it

101 of 102

Conditionals: Can this ever return True?

T

T

F

T

F

F

T

F

T

F

F

F

T

F

T

T

F

T

T

F

F

T

T

T

F

F

T

F

T

T

T

T

  • So this function never returns True!

102 of 102

Q&A + Feedback Form

  • Link: tinyurl.com/fa25-disc-form

See you next week!