Docsity

CSSE 120: Loops, Parameters, and Function Calls, Study notes of Software Engineering

Partial preview of the text

Download CSSE 120: Loops, Parameters, and Function Calls and more Study notes Software Engineering in PDF only on Docsity!

PARAMETERS, INDEFINITE

LOOPS, AND LOOP PATTERNS

CSSE 120—Rose Hulman Institute of Technology

Choose a partner today

 During session 13, we’ll be doing Tic Tac Toe in pairs. Sometime before you leave today, please find a partner and write both of your usernames next to each other on the sheet that will be passed around

 This will allow us to create an SVN repository for each pair.

Functions mutating parameters

 Can we write a function that exchanges the values of its two parameters?  In Eclipse checkout the project named Session11 from your SVN repository  Study the code in the module mutatingParameters.py but don’t run it  Together, observe what happens as we trace its execution in the debugger

Q

Modifying Parameters

 How do functions send information back?

 Return statements  Mutating parameters  Value of actual parameter must be a mutable object  State of the mutable object is changed  The actual parameter itself is NOT changed since it refers to the same object  Parameter is still passed by value

Q

Some indefinite loop patterns

 Interactive loops

 Sentinel loops

 File loops

 post-test loops

 "loop and a half"

Q

Interactive: Make the user count

 Open module averageUserCount.py and execute it together

 When does the loop terminate?

 Is this the best way to make the user enter input?

 Why?  Why not?

Sentinel loop

 Open module averageSentinel.py and study the code then execute it together

 User signals end of data by a special "sentinel“ value

 Note that the sentinel value is not used in calculations

Q

Non-numeric Sentinel

 What if negative numbers are legitimate values?

 Open module averageOtherSentinel.py and study the code  Execute it together  What is the sentinel?

Again note: sentinel value is not used in calculations.

Interactive loop with graphics

 Display a window that contains a circle and a message saying "Click inside Circle".

 Whenever the user clicks outside the circle, display "You missed!"

 If the user clicks inside the circle, display "Bull's eye!". Then pause and close the window.

 Implement together in module clickInsideCircle.py

Individual Exercise on Using loops

 Define function listAndMax() in module listMax.py that  Prompts the user to enter numbers, one at a time  Uses a blank line () as sentinel to terminate input  Accumulates the numbers in a list  Uses a loop to calculate the maximum value of the numbers  Returns two values:  the list of numbers entered in the order they were entered  the maximum value

 Define function main() in module listMax.py that  Calls listAndMax()  Prints the list of numbers entered  Prints the maximum value of the list of numbers Q