Download presentation
Presentation is loading. Please wait.
Published byCassidy Goodyear Modified over 9 years ago
1
Q and A for Chapter 6 CS 104 Victor Norman
2
Return values Q: A function definition that returns a value (i.e., a non-void function) must have at least one of what kind of statement? A: return statement like this return somevalue
3
Return vs. print Q: Why do you have to return a value? Why not just print it? A: Suppose sin(x) doesn’t return the value, but prints it only. How would you do this?: sin(x) ** 2 cos(sin(x)) Printed values are sent out to the screen, but cannot be used in later computations.
4
Multiple return statements Q: When do you need to have multiple return statements in a function definition? A: When your entire code is an if-else block, then each “branch” of the conditional must have a return statement. NOTE: always best for all returns to return a value of the same type.
5
Return’s meanings Q: What 2 things does a return statement do? A: 1. Specifies the value to be returned 2. Makes the function return immediately. Code below it is not run. NOTE: you cannot put return in the main code. It must be in a function definition.
6
Dead code Q and A: Identify the dead code below: def computeIQ(yrsInSchool, dadsIQ, momsIQ): if dadsIQ == 0: iq = yrsInSchool * momsIQ return iq dadsIQ = momsIQ else: return (dadsIQ + momsIQ) * yrsInSchool return 100.0# return default IQ.
7
Return multiple values? Q: Can a function return multiple values at once? A: No. A function can return only one thing. But, it could be a thing that holds multiple values, like a list or a tuple…
8
What happens to a returned value? Q: What happens to a returned value? Does it just get printed? A: The returned value must be stored in a variable or printed out explicitly. Doing this: y = sin(x) stores the result in y. Doing this: sin(x) does nothing (in script mode): the returned value is lost.
9
Incremental Development Q: In the text, the author talks about incremental development. What does he mean by this? A: He means writing new code 1 line at a time (sometimes), and testing your code each time. It helps the n00b write better code faster.
10
Boolean function practice 1 Q: Write a function isIsosceles() that takes 3 floats as parameters and returns True if two of the side lengths are the same, and False otherwise. A: def isIsosceles(s1, s2, s3): return (s1 == s2 or s1 == s3 or s2 == s3)
11
Boolean function practice 2 Q: Write a function isScalene() that takes 3 floats as parameters and returns True if no two sides have the same length. A: def isScalene(s1, s2, s3): return (s1 != s2 and s1 != s3 and s2 != s3)
12
Boolean function practice 3 Q: Write a function isEquilateral() that takes 3 floats as parameters and returns True if all sides have the same length. A: def isEquilateral(s1, s2, s3): return (s1 == s2 == s3)
13
Boolean function practice 4 Q: Write a function isNotEquilateral() that calls isEquilateral() but returns True if the 3 sides are all not the same. A: def isNotEquilateral(s1, s2, s3): return not isEquilateral(s1, s2, s3)
14
“Leap of Faith” section Q: What does the author mean by this “leap of faith”? I don’t get it. A: The author means that when checking code, you just assume a call to a function returns or does the right thing – you don’t examine the code thoroughly every time. You can do this when designing code, too: just write code with function calls that you haven’t implemented yet, and then implement them later.
15
isinstance() Q: I don’t understand this isinstance() stuff… A: That’s not a question… It takes two parameters: isinstance(variablename, typename) and returns a Boolean value (True or False) Useful for defensive programming: checking to see if args to a function are of the correct type.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.