Skip to content

6.9 Functions: Common Errors

Copy-paste errors

  • A common error is to copy-and-paste code among functions but then not complete all necessary modifications to the pasted code.
    • For example, a programmer might have developed and tested a function to convert a temperature value in Celsius to Fahrenheit, and then copied and modified the original function into a new function to convert Fahrenheit to Celsius as shown:

The programmer forgot to change the return statement to return Celsius rather than Fahrenheit.

The programmer forgot to change the return statement to return Celsius rather than Fahrenheit.

Return Errors

  • Another common error is to return the wrong variable, like if return temperature had been used in the temperature conversion program by accident. The function will work and sometimes even return the correct value.
  • Another common error is to fail to return a value for a function.

6%209%20Functions%20Common%20Errors%20f1d39f5463444811bf4097bf64235e99/Untitled%201.png