M&C1:  Incorrect use of the return statement and value returned

Most functions your students write will have a return statement.  Common mistakes students make when starting to write functions relate to misconceptions students have on how computed values are managed. These include:

  • Printing the computed value instead of returning it.  In the testing/debugging state, one may print in addition to returning the value. However, in order for the computed value to be used in later computations it needs to be returned. Students often get confused between printing something to the terminal and returning a value, and it is a good idea to discourage students from using I/O inside functions.
  • When calling a function, returning a value but not storing it for later reference.  For example, instead of
    myArea = computeCircleArea(px, py, qx, qy)
    typing
    computeCircleArea(px, py, qx, qy)
  • Not realizing that a function with no return statement returns None (which has the potential to lead to errors later in the code).

In general, it is a good practice to limit the number of return statements in functions.