Understanding Python Function Calls and Handling Call Frames in Programming Exams

Slide Note
Embed
Share

Topics covered include concepts of call frames, drawing variables, and objects to understand function calls in Python programming. Learning these skills helps in error detection, critical thinking, and general programming proficiency. The content emphasizes the importance of drawing call frames and variables to execute statements correctly. Examples from previous exams are highlighted to aid comprehension.


Uploaded on Aug 30, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Call Frames Final Review Spring 2018 CS 1110

  2. The Big Issue Cannot answer questions on this topic without drawing variables drawing frames for function calls drawing objects when they are created Learning to do this is useful in general Helps you think like a computer Easier to find errors in your programs.

  3. What Do You Need to Know? Major topics local variables (in a function body) function call (call frames, call stack) class folders, inheritance, and constructor calls Examples from previous exams Question 3 on prelim 1 Question 6 on prelim 2

  4. Important Code execution is an important part of the final You need to know how to draw variables draw call frames draw objects The purpose of such questions on executing statements with constructs and function calls is to test your understanding of how Python programs are executed.

  5. The Frame (box) for a Function Call Function Frame: Representation of function call A conceptual model of Python Number of statement in the function body to execute next Starts with function s first executable statement Draw parameters as variables (named boxes) function name instruction counter parameters local variables (when assigned)

  6. Iterative Example Call Stack: def countVowels(word): 1 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi word 1 7 return count Global Space: Call: e = count_vowels( hi )

  7. Iterative Example Call Stack: def countVowels(word): 1, 2 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi word 1 x 0 0 7 return count Global Space: Call: e = count_vowels( hi )

  8. Iterative Example Call Stack: def countVowels(word): 1, 2, 3 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi 0 0 word count 1 x 0 0 7 return count Global Space: Call: e = count_vowels( hi )

  9. Iterative Example Call Stack: def countVowels(word): 1, 2, 3, 4 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi 0 0 word count 1 x 0 0 7 return count Global Space: Call: e = count_vowels( hi )

  10. Iterative Example Call Stack: def countVowels(word): 1, 2, 3, 4, 6 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi 0 0 word count 1 x 0 0 7 return count Global Space: Call: e = count_vowels( hi )

  11. Iterative Example Call Stack: def countVowels(word): 1, 2, 3, 4, 6, 3 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi 0 0 word count 1 x 0 0 1 1 7 return count Global Space: Call: e = count_vowels( hi )

  12. Iterative Example Call Stack: def countVowels(word): 1, 2, 3, 4, 6, 3, 4 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi 0 0 word count 1 x 0 0 1 1 7 return count Global Space: Call: e = count_vowels( hi )

  13. Iterative Example Call Stack: def countVowels(word): 1, 2, 3, 4, 6, 3, 4, 5 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi 0 0 word count 1 x 0 0 1 1 7 return count Global Space: Call: e = count_vowels( hi )

  14. Iterative Example Call Stack: def countVowels(word): 1, 2, 3, 4, 6, 3, 4, 5, 6 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi 0 0 1 1 word count 1 x 0 0 1 1 7 return count Global Space: Call: e = count_vowels( hi )

  15. Iterative Example Call Stack: def countVowels(word): 1, 2, 3, 4, 6, 3, 4, 5, 6, 3 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi 0 0 1 1 word count 1 x 0 0 1 1 2 2 7 return count Global Space: Call: e = count_vowels( hi )

  16. Iterative Example Call Stack: def countVowels(word): 1, 2, 3, 4, 6, 3, 4, 5, 6, 3, 7 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi 0 0 1 1 word count 1 x 0 0 1 1 2 2 7 return count Global Space: Call: e = count_vowels( hi )

  17. Iterative Example Call Stack: def countVowels(word): 1, 2, 3, 4, 6, 3, 4, 5, 6, 3, 7 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi 0 0 1 1 word count 1 x RETURN 1 1 0 0 1 1 2 2 7 return count Global Space: Call: e = count_vowels( hi )

  18. Iterative Example Call Stack: def countVowels(word): 1, 2, 3, 4, 6, 3, 4, 5, 6, 3, 7 countVowels countVowels """Returns: The number of vowels in the string s. Precondition: s is a string""" x = 0 2 count = 0 3 while x < len(word): 4 if word[x] in [ a , e , i , o , u ]: 5 count += 1 6 x += 1 hi hi 0 0 1 1 word count 1 x RETURN 1 1 0 0 1 1 2 2 7 return count e Global Space: 1 1 Call: e = count_vowels( hi )

  19. Subcall Example Call Stack: 1 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list def printWeather(wkList): 1 for item in wkList: 0 Rainy Rainy 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny if day == Sunny : 3 print ( Time for a picnic! ) 4 if day == Rainy : 5 print ( Grab your umbrella! ) 6 Call: printWeather(a)

  20. Subcall Example Call Stack: 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list Rainy Rainy item def printWeather(wkList): 1 for item in wkList: 0 Rainy Rainy 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny if day == Sunny : 3 print ( Time for a picnic! ) 4 if day == Rainy : 5 print ( Grab your umbrella! ) 6 Call: printWeather(a)

  21. Subcall Example Call Stack: 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list Rainy Rainy item def printWeather(wkList): 1 for item in wkList: 0 3 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 print ( Time for a picnic! ) 4 if day == Rainy : 5 print ( Grab your umbrella! ) 6 Call: printWeather(a)

  22. Subcall Example Call Stack: 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list Rainy Rainy item def printWeather(wkList): 1 for item in wkList: 0 3, 5 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 print ( Time for a picnic! ) 4 if day == Rainy : 5 print ( Grab your umbrella! ) 6 Call: printWeather(a)

  23. Subcall Example Call Stack: 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list Rainy Rainy item def printWeather(wkList): 1 for item in wkList: 0 3, 5, 6 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 print ( Time for a picnic! ) 4 if day == Rainy : 5 print ( Grab your umbrella! ) 6 Call: printWeather(a)

  24. Subcall Example Call Stack: 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list Rainy Rainy item def printWeather(wkList): 1 for item in wkList: 0 3, 5, 6 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 None None RETURN print ( Time for a picnic! ) 4 if day == Rainy : 5 print ( Grab your umbrella! ) 6 Call: printWeather(a) Output: Grab your umbrella!

  25. Subcall Example Call Stack: 1, 2, 1 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list Rainy Rainy item def printWeather(wkList): 1 for item in wkList: 0 3, 5, 6 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 None None RETURN print ( Time for a picnic! ) 4 if day == Rainy : 5 print ( Grab your umbrella! ) 6 Call: printWeather(a) Output: Grab your umbrella!

  26. Subcall Example Call Stack: 1, 2, 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list Rainy , Rainy , Sunny Sunny item def printWeather(wkList): 1 for item in wkList: 0 3, 5, 6 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 None None RETURN print ( Time for a picnic! ) 4 if day == Rainy : 5 print ( Grab your umbrella! ) 6 Call: printWeather(a) Output: Grab your umbrella!

  27. Subcall Example Call Stack: 1, 2, 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list Rainy , Rainy , Sunny Sunny item def printWeather(wkList): 1 for item in wkList: 0 3, 5, 6 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 None None RETURN print ( Time for a picnic! ) 4 if day == Rainy : 5 3 dayWeather dayWeather print ( Grab your umbrella! ) 6 day Sunny Sunny Call: printWeather(a) Output: Grab your umbrella!

  28. Subcall Example Call Stack: 1, 2, 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list Rainy , Rainy , Sunny Sunny item def printWeather(wkList): 1 for item in wkList: 0 3, 5, 6 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 None None RETURN print ( Time for a picnic! ) 4 if day == Rainy : 5 3, 4 dayWeather dayWeather print ( Grab your umbrella! ) 6 day Sunny Sunny Call: printWeather(a) Output: Grab your umbrella!

  29. Subcall Example Call Stack: 1, 2, 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list Rainy , Rainy , Sunny Sunny item def printWeather(wkList): 1 for item in wkList: 0 3, 5, 6 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 None None RETURN print ( Time for a picnic! ) 4 if day == Rainy : 5 3, 4, 5 dayWeather dayWeather print ( Grab your umbrella! ) 6 day Sunny Sunny Call: printWeather(a) Output: Grab your umbrella!

  30. Subcall Example Call Stack: 1, 2, 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list Rainy , Rainy , Sunny Sunny item def printWeather(wkList): 1 for item in wkList: 0 3, 5, 6 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 None None RETURN print ( Time for a picnic! ) 4 if day == Rainy : 5 3, 4, 5 dayWeather dayWeather print ( Grab your umbrella! ) 6 day Sunny Sunny Call: printWeather(a) None None RETURN Output: Grab your umbrella! Time for a picnic!

  31. Subcall Example Call Stack: 1, 2, 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: wkList id1 id1 id1 list Rainy , Rainy , Sunny Sunny item def printWeather(wkList): 1 for item in wkList: 0 3, 5, 6 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 None None RETURN print ( Time for a picnic! ) 4 if day == Rainy : 5 3, 4, 5 dayWeather dayWeather print ( Grab your umbrella! ) 6 day Sunny Sunny Call: printWeather(a) None None RETURN Output: Grab your umbrella! Time for a picnic!

  32. Subcall Example Call Stack: 1, 2, 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: None None wkList id1 id1 RETURN id1 list Rainy , Rainy , Sunny Sunny item def printWeather(wkList): 1 for item in wkList: 0 3, 5, 6 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 None None RETURN print ( Time for a picnic! ) 4 if day == Rainy : 5 3, 4, 5 dayWeather dayWeather print ( Grab your umbrella! ) 6 day Sunny Sunny Call: printWeather(a) None None RETURN Output: Grab your umbrella! Time for a picnic!

  33. Subcall Example Call Stack: 1, 2, 1, 2 printWeather printWeather a id1 id1 Heap Space: Global Space: None None wkList id1 id1 RETURN id1 list Rainy , Rainy , Sunny Sunny item def printWeather(wkList): 1 for item in wkList: 0 3, 5, 6 Rainy Rainy dayWeather dayWeather 2 dayWeather(item) 1 def dayWeather(day): Sunny Sunny day Rainy Rainy if day == Sunny : 3 None None RETURN print ( Time for a picnic! ) 4 if day == Rainy : 5 3, 4, 5 dayWeather dayWeather print ( Grab your umbrella! ) 6 day Sunny Sunny Call: printWeather(a) None None RETURN Output: Grab your umbrella! Time for a picnic!

  34. Diagramming Objects (Folders) Object Folder Class Folder Folder Name (arbitrary) No folder name id4 classname classname Class Attributes Method Names Instance Attributes Draw attributes as named box w/ value Parameters are required in methods

  35. Evaluation of a Constructor Call 3 steps to evaluating the call C(args) Create a new folder (object) of class C Give it with a unique name (any number will do) Folder goes into heap space Execute the method __init__(args) Yield the name of the object as the value A constructor call is an expression, not a command Does not put name in a variable unless you assign it

  36. Diagramming Subclasses Important Details: Make sure you put the superclass-name in parentheses Do not duplicate inherited methods and attributes Include initializer and and other special methods (as applicable) Method parameters are required Class attributes are a box with (current) value superclass-name Declared in Superclass: Class Attributes Method Names subclass-name(super) Declared in Subclass: Class Attributes Method Names

  37. Two Example Classes class B(A): y=4 z=10 def __init__(self,x,y): super().__init__(y) self.x = x class A(object): x=3 y=5 def __init__(self,y): self.y = y def f(self): return self.g() def g(self): return self.x+self.z def h(self): return 42 def g(self): return self.x+self.y

  38. Class Folders A A __init__(self,x) __init__(self,x) These folders will still exist in the following slides, but will not be redrawn; they exist in the heap space along with the object folders. 3 5 5 3 x y y x f(self) f(self) g(self) g(self) B(A) __init__(self,x,y) x y 4 10 g(self) h(self)

  39. Constructor Examples Call Stack: 1 A.__init__ A.__init__ Heap Space: Global Space: self id8 id8 id8 A 1 1 y class A(object): x = 3 y = 5 def __init__(self,y): self.y = y 1 class B(A): y = 4 z = 10 def __init__(self,x,y): super().__init__(y) self.x = x 2 3 Call: a = A(1) b = B(7, 3)

  40. Constructor Examples Call Stack: 1 A.__init__ A.__init__ Heap Space: id8 Global Space: id8 id8 self id8 id8 RETURN A 1 1 y class A(object): x = 3 y Rainy Rainy 1 1 y = 5 def __init__(self,y): self.y = y 1 class B(A): y = 4 z = 10 def __init__(self,x,y): super().__init__(y) self.x = x 2 3 Call: a = A(1) b = B(7, 3)

  41. Constructor Examples Call Stack: 1 A.__init__ A.__init__ a id8 id8 Heap Space: id8 Global Space: id8 id8 self id8 id8 RETURN A 1 1 y class A(object): x = 3 y Rainy Rainy 1 1 y = 5 def __init__(self,y): self.y = y 1 class B(A): y = 4 z = 10 def __init__(self,x,y): super().__init__(y) self.x = x 2 3 Call: a = A(1) b = B(7, 3)

  42. Constructor Examples Call Stack: 1 A.__init__ A.__init__ a id8 id8 Heap Space: id8 Global Space: id8 id8 self id8 id8 RETURN A 1 1 y class A(object): x = 3 y Rainy Rainy 1 1 y = 5 2 B.__init__ B.__init__ def __init__(self,y): self.y = y 7 7 self id4 id4 x 1 3 3 class B(A): y = 4 z = 10 def __init__(self,x,y): super().__init__(y) self.x = x y id4 B 2 3 Call: a = A(1) b = B(7, 3)

  43. Constructor Examples Call Stack: 1 A.__init__ A.__init__ a id8 id8 Heap Space: id8 Global Space: id8 id8 self id8 id8 RETURN A 1 1 y class A(object): x = 3 y Rainy Rainy 1 1 y = 5 2 B.__init__ B.__init__ def __init__(self,y): self.y = y 7 7 self id4 id4 x 1 3 3 class B(A): y = 4 z = 10 def __init__(self,x,y): super().__init__(y) self.x = x y id4 B 1 A.__init__ A.__init__ 2 3 self id4 id4 3 3 y Call: a = A(1) b = B(7, 3)

  44. Constructor Examples Call Stack: 1 A.__init__ A.__init__ a id8 id8 Heap Space: id8 Global Space: id8 id8 self id8 id8 RETURN A 1 1 y class A(object): x = 3 y Rainy Rainy 1 1 y = 5 2 B.__init__ B.__init__ def __init__(self,y): self.y = y 7 7 self id4 id4 x 1 3 3 class B(A): y = 4 z = 10 def __init__(self,x,y): super().__init__(y) self.x = x y id4 B 1 A.__init__ A.__init__ 2 3 id4 id4 self id4 id4 RETURN y 3 3 3 3 y Call: a = A(1) b = B(7, 3)

  45. Constructor Examples Call Stack: 1 A.__init__ A.__init__ a id8 id8 Heap Space: id8 Global Space: id8 id8 self id8 id8 RETURN A 1 1 y class A(object): x = 3 y Rainy Rainy 1 1 y = 5 2, 3 B.__init__ B.__init__ def __init__(self,y): self.y = y 7 7 self id4 id4 x 1 3 3 class B(A): y = 4 z = 10 def __init__(self,x,y): super().__init__(y) self.x = x y id4 B 1 A.__init__ A.__init__ 2 3 id4 id4 self id4 id4 RETURN y 3 3 3 3 y Call: a = A(1) b = B(7, 3)

  46. Constructor Examples Call Stack: 1 A.__init__ A.__init__ a id8 id8 Heap Space: id8 Global Space: id8 id8 self id8 id8 RETURN A 1 1 y class A(object): x = 3 y Rainy Rainy 1 1 y = 5 2, 3 B.__init__ B.__init__ def __init__(self,y): self.y = y 7 7 7 7 x self id4 id4 x 1 3 3 class B(A): y = 4 z = 10 def __init__(self,x,y): super().__init__(y) self.x = x y id4 id4 RETURN id4 B 1 A.__init__ A.__init__ 2 3 id4 id4 self id4 id4 RETURN y 3 3 3 3 x 7 7 y Call: a = A(1) b = B(7, 3)

  47. Constructor Examples Call Stack: 1 A.__init__ A.__init__ a id8 id8 Heap Space: id8 Global Space: b id8 id8 self id8 id8 RETURN id4 id4 A 1 1 y class A(object): x = 3 y Rainy Rainy 1 1 y = 5 2, 3 B.__init__ B.__init__ def __init__(self,y): self.y = y 7 7 7 7 x self id4 id4 x 1 3 3 class B(A): y = 4 z = 10 def __init__(self,x,y): super().__init__(y) self.x = x y id4 id4 RETURN id4 B 1 A.__init__ A.__init__ 2 3 id4 id4 self id4 id4 RETURN y 3 3 3 3 x 7 7 y Call: a = A(1) b = B(7, 3)

Related


More Related Content