Chapter 6 Return Values
Built-in Functions
Built-in Functions: Functions that are pre-defined in Python.
Common examples include:
abs(): Returns the absolute value of a number.round(): Rounds a number to a specified number of decimal places.Math Module Functions: E.g.,
math.sqrt(),math.pow().math.sqrt(x): Returns the square root of x.math.pow(x, y): Returns x raised to the power of y.
Return Values
Functions can return values that can be assigned to variables or used as part of expressions.
Example:
a = circle_area(radius)orcircle_area(radius) + 2 * circle_area(radius / 2).
Local Variables: Variables defined within a function (e.g.,
areaincircle_area) cannot be accessed outside that function.Some functions may not return a value (
None), which represents a lack of value:Noneis similar toTrueandFalse, but typically represents a non-existent or empty return.
Pure Functions: Functions that only return a value without any other side effects.
Conditional Return Values
Rewrite functions like
absmust ensure all code paths hit a return statement.Example of unsafe code includes paths where there is no return value leading to
None.
Dead Code: Portions of code that cannot execute, often because they follow a return statement.
Incremental Development
An approach in coding where development occurs in small increments with testing after each addition.
Sample arguments should replace print statements with return values to prevent returning
None.This method helps in ensuring that functions remain pure and effective.
Scaffolding
Scaffolding: Temporary code used during development that helps in debugging but is not part of the finished product.
Effective for troubleshooting code and reducing debugging time.
Boolean Functions
Functions that return boolean values (
TrueorFalse).They are useful for complex tests especially in conditionals.
Recursion and Return Values
Recursion involves a function calling itself and is foundational to many coding problems.
Turing Complete: A language is this if it can perform any computation that can be described algorithmically.
Writing recursive definitions similar to mathematical equality can help create powerful Python functions.
A challenge arises when ensuring recursive definitions efficiently calculate results.
Checking Types
To prevent infinite recursion, use
isinstance()to check the type of arguments received by functions.
Input Validation
Input Validation: Process of checking parameters to ensure they are of the correct type and hold expected values.
Debugging Functionality
If functions fail, consider these problems:
Issues with argument types (PREcondition violated).
Issues within the function logic (POSTcondition violated).
Mismanagement of returned values by the caller.
Debugging steps include:
Use print statements to check PREconditions at function entry.
Insert print statements prior to each return statement.
Review the function calls to ensure return values are correct and being utilized consistently.
Glossary
Return value: The result produced by a function.
Pure Function: No side effects; only returns a value.
Dead Code: Code that is unreachable, often due to a prior return statement.
Incremental Development: Strategy of adding code in small, testable increments.
Scaffolding: Temporary support code used during development.
Turing Complete: Indicates a computational system capable of performing any algorithmic computation.
Input Validation: Ensuring function parameters are of correct types and values.