Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Functions with Return Values CS303E: Elements of Computers and Programming.

Similar presentations


Presentation on theme: "More Functions with Return Values CS303E: Elements of Computers and Programming."— Presentation transcript:

1 More Functions with Return Values CS303E: Elements of Computers and Programming

2 Announcements Exam 2: Wednesday, April 3 rd Exam 2: Wednesday, April 3 rd During regular class time – NOT in this classroom. During regular class time – NOT in this classroom. Location will be posted on course webpage. Location will be posted on course webpage.

3 Reminder: Variable Scope and Functions The scope of a variable is the part of the program in which that variable is accessible The scope of a variable is the part of the program in which that variable is accessible A function parameter’s scope is its function A function parameter’s scope is its function

4 Pass By Value A function cannot change the value of the argument that is passed to it A function cannot change the value of the argument that is passed to it –The value of the argument in the calling statement does not change, no matter how the function manipulates the parameter Some programming languages do this, others use pass by reference Some programming languages do this, others use pass by reference –It is important to find out which!

5 Pass By Value: Example def main(): arg1=35 arg1=35 print “arg1 is”, arg1 print “arg1 is”, arg1 changeIt(arg1) changeIt(arg1) print “after call,” +\ “arg1 is”, arg1 print “after call,” +\ “arg1 is”, arg1 def changeIt(par): #recall that initially #recall that initially #par is set to the #value of arg1 #par is set to the #value of arg1 par=-1 par=-1 print “par is”, par print “par is”, par What happens when this program executes?

6 Return By Value The same applies to return statements The same applies to return statements The calling program cannot modify the variable from the function The calling program cannot modify the variable from the function Typical in all programming languages Typical in all programming languages

7 Question What does pass by value mean? A. Changes to a function’s argument are reflected in the calling function B. Changes to a function’s argument are NOT reflected in the calling function

8 Returning Multiple Values A function may return a tuple which is a collection of values. A function may return a tuple which is a collection of values. –So… it’s not really multiple values, but one value that is itself a collection of values Example: return a,b return a,b But how do you assign multiple values?

9 Simultaneous Assignment Use simultaneous assignment to assign multiple values in one statement Use simultaneous assignment to assign multiple values in one statementa,b=2,3 print “a=%d b=%d” % (a,b) Output: a=2 b=3

10 Returning Multiple Values: Example def divide(a,b) #return dividend and remainder in a tuple #return dividend and remainder in a tuple return a/b, a%b return a/b, a%b def main(): div,rem=divide(13,8) div,rem=divide(13,8) print “%d/%d=%d R %d” % (13,8,div,rem) print “%d/%d=%d R %d” % (13,8,div,rem)

11 Question: What are the values of a and b? What are the values of a and b? def add_sub(x,y): return x+y,x-y return x+y,x-y def main(): a=4 a=4 b=5 b=5 a,b=add_sub(a,b) a,b=add_sub(a,b) A. a=4 b=5 B. a=9 b=9 C. a=9 b=-1 D. a=-1 b=9

12 Exercise Write a function minMax() that takes 2 arguments, a and b, and returns a tuple containing the minimum and mximum of a and b, in that order. Then write a main function that calls minMax() on all combinations of inputs a and b such that 1<=a<=5, 0<=b<=3.

13 Group Exercise Write a program to simulate a digital clock. Your program should print the values of each second like this: 00:00:0000:00:0100:00:02…23:59:59 You will need the following code: import time time.sleep(1) #tells #program to wait for 1 #second before continuing

14 Group Exercise Write a program that quizzes the user on addition until the user enters “done”. Your program should generate two random numbers between 1 and 1,000 and print them to the screen (like this: a + b = ) and then allow the user to enter an answer. The program should report whether the answer is right or wrong and give the correct answer if it is wrong. You’ll need: random.randint(1,1000)


Download ppt "More Functions with Return Values CS303E: Elements of Computers and Programming."

Similar presentations


Ads by Google