1 / 20

Q and A for Section 5.2

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>

rafe
Download Presentation

Q and A for Section 5.2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Q and A for Section 5.2 CS 106

  2. 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”)

  3. 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.

  4. 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?...

  5. 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.

  6. 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?

  7. 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!

  8. 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.

  9. 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.

  10. Q10 Q: Will this work? def average(x, y): return (x + y) / 2.0 z = 10 q = average(z, z) A: Yes. Why not?!

  11. 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.

  12. 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().

  13. Q13 Q: When might you write and use a function that doesn't return anything? A: Perhaps the function prints out some stuff.

  14. 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.

  15. Q15 Q: Can a function call another function? Can a function call itself? A: Of course! And, yes!

  16. 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.

  17. 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.

  18. 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-')

  19. 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)

  20. 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)

More Related