Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 8 Online
OpenStudy (anonymous):

I don't understand this piece of code below. We do have 3 arguments in the function 'close'. So how come while calling the function, we have (10*x, numIters). I really don't get the '10*x' def close(x, y, epsilon = 0.00001): return abs(x - y) < epsilon if close(10.0*x, numIters): print 'Good enough' Yea and its from Lecture7 6.00. Thanks

OpenStudy (mandre):

the 3rd argument has a default value, so you do not need to add it as an argument. If you do add a 3rd argument it will overwrite the default value. I assume when you call close, x and numIters will have values. I'm not sure why you don't "get" 10.0*x. It means 10 times x as far as I can tell. The function simply compares 2 numbers to see how close they are to each other in value. close(20.01, 20.0, 0.1) will return "Good enough" as the numbers are close together using your epsilon value of 0.01. 20.01 - 20.0 = 0.01 which is less than 0.1.

OpenStudy (anonymous):

Mandre is right. Assuming x and numlters are assigned to some values, your call code is basically taking 10x and numlters as its arguments. So, if 10x - numlters < epsilon, your code should print "Good Enough". I think you're probably confused about the usage of x in both the function definition and later in the call function. In the definition, x doesn't have a value per se. It's just there as a label, if I may call it that, to tell the computer what to do with "x" when you finally call the function with some value it can work with. You can rewrite the function definition as: def close(z, y, epsilon = 0.00001): return abs(z - y) < epsilon And later call it with x (considering x has a value); it's still going to work. Try it.

OpenStudy (anonymous):

Yea I was quite confused about the usage of 10*x. And it skipped my mind that u don't need to include any parameter while calling a function if a value is already assigned to the function parameter like 'epsilon = 0.00001'in close function. Thanks alot

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!