200 likes | 285 Views
Q and A for Section 5.2 . CS 106. Function definition syntax. Q: The syntax for the definition of a function is: def ______________( _______________ ): _____________ A: def < functionname > ( < param list> ): <body>
E N D
Q and A for Section 5.2 CS 106
Function definition syntax Q: The syntax for the definition of a function is: def ______________( _______________ ): _____________ A: def <functionname> ( <param list> ): <body> Note: <param list> is a comma-separated list of identifiers, that will refer to the values passed in. (“identifiers” <--> “variables”)
What does this function do? Q: What does this function do? def unknown(val1, val2): tmp = val1 ** val2 return tmp A: It is the power function. Note: tmp is a local variable– only defined and accessible within the function.
Function call example Q: Write code to call unknown() and print the results. A: print "An unknown result: ", unknown(2.5, 3.5) What if unknown() had printed a result instead of returning it?...
return statement Q: What does return <val>do? A: It returns the value <val> to the caller, and, ends execution of code in the function. Q: How is this different than printing a value in a function? A: Printing sends out characters to the screen, but does not give a value back to the caller.
Q6 Q: What is the output from this code: def f(x): x += 3 print x z = 10 f(z) print z A: Output is 13 on one line, then 10 on the next. Why?
Q7 Q: What does z hold at the end of this code?: def f(x): x += 3 print x y = 7 z = f(y) A: z holds the value None, because the function f() does not return anything. NOTE: printing a value is NOT the same as returning a value!
Q8 Q: If you define a variable inside a function, can you access that variable from outside the function? Explain. A: No, you cannot access it. Once the code stops running the function, the local variables cease to exist.
Q9 Q: Fix the syntax errors below: def average(xy) z = (x + y) / 2.0 return z print avrage(3, 77) A: need comma between x and y; need colon at end of line 1; return z is mis-indented; call to average is misspelled.
Q10 Q: Will this work? def average(x, y): return (x + y) / 2.0 z = 10 q = average(z, z) A: Yes. Why not?!
Q11 Q: In this code, identify the formal parameters and the actual parameters. def average(x, y): return (x + y) / 2.0 z = 10 q = average(z, z) A: x, y are the formal parameters. z and z are the actual parameters.
Q12 Q: Is this legal? def average(x, y): return (x + y) / 2.0 y = 10 x = 20 print average(y, x) A: Yes. The value of y is assigned to x in average(). The value in x is assigned to y in average().
Q13 Q: When might you write and use a function that doesn't return anything? A: Perhaps the function prints out some stuff.
Q14 Q: Is this legal? def average(x, y): return (x + y + z) / 3.0 z = 100000 print average(0, 1) A: It is legal. A function can access a variable in the global scope. This is often not very desirable, however.
Q15 Q: Can a function call another function? Can a function call itself? A: Of course! And, yes!
Q16 Q: Does a person have to know how a function does something in order to use it? A: No! A function defines what it does, and the caller doesn't care how it does it.
Q17 Q: How does a person know what a function can do for you? A: The writer of the function gives the function a nice descriptive name, with good formal parameter names, and a docstring (or comments) to describe what the function does.
Q18 Q: If you are given this function, show two ways you could call it, with different numbers of arguments: def plot_points(x_list, y_list, symbol='ro') A: plot_points(xs, ys) plot_points(xs, ys, 'g-')
Q19 Q: Write the signature of a function draw() that takes x, y, and radius as 3 required parameters, and takes canvas and width as optional parameters, with the default value for canvas being canv and the default value for width being 2. A: draw(x, y, radius, canvas=canv, width=2)
Q20 Q: For the previous draw() function, could a caller provide a width value without specifying (or overriding)the canvas value? A: Actually, yes. You have to use keyword arguments: draw(3, 10, 14, width=7) or, e.g., draw(x=3, y=10, radius=14, width=7) or, draw(width=10, y=1, radius=7, x=1000)