Week 6 - Return Values
Understanding Return Values and Functions in Python¶
Overview¶
This lesson focuses on teaching return values in Python functions. It explores how functions return values, conditional returns, recursion with return values, input validation, and debugging. By the end of the lesson, you should have a clear understanding of return values and how to apply them effectively.
Objectives¶
- Improve your skill in writing functions that return values.
- Differentiate between functions that return
None
and those that return values. - Understand how return values work with conditional statements.
- Explore recursive functions with return values.
- Apply input validation in functions.
- Practice incremental development for debugging.
Introduction to Return Values¶
Concept: As you have probably figured out, a function can return a value that can be assigned to a variable or used in expressions.
Example Code:¶
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Key Takeaway: Return values allow functions to pass results back to the caller.
Return Values vs. None¶
Concept: If a function doesn’t have a return
statement, it returns None
by default.
Example Code:¶
1 2 3 4 5 |
|
Improved Version:¶
1 2 3 4 5 |
|
Key Takeaway: Functions without a return
statement return None
. Always use return
for reusable results.
Return Values and Conditionals¶
Concept: Conditional statements within a function should ensure EVERY possible execution path has a return
statement.
Example Code:¶
1 2 3 4 |
|
Incorrect Code Example:¶
1 2 3 4 5 6 |
|
Key Takeaway: Make sure every execution path hits a return
statement.
Incremental Development¶
Concept: Write small, testable functions and build them incrementally.
Example Code:¶
1 2 3 4 5 6 7 8 9 |
|
Key Takeaway: Build and test small pieces of code to avoid larger debugging issues.
Boolean Functions¶
Concept: Functions can return True
or False
based on conditions.
Example Code:¶
1 2 3 4 5 6 |
|
Key Takeaway: Boolean functions simplify complex conditional logic.
Recursion with Return Values¶
Concept: Recursive functions can use return values to build up solutions.
Example Code:¶
1 2 3 4 5 6 7 8 |
|
Key Takeaway: Recursive functions are powerful, but ensure they have valid base cases to terminate.
Input Validation¶
Concept: Validate inputs to prevent unexpected behavior.
Example Code:¶
1 2 3 4 5 6 7 8 9 10 11 |
|
Key Takeaway: Input validation ensures the function works with expected data types and values.
Debugging with Scaffolding¶
Concept: Use print statements to track flow of execution and return values during debugging.
Example Code:¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Key Takeaway: Use scaffolding to debug complex functions by visualizing intermediate steps.
Exercises¶
- Modify the
distance
function to calculate the 3D distance between points. - Write a function
is_prime(n)
that returnsTrue
ifn
is prime andFalse
otherwise.- Here’s a basic approach, known as trial division:
- If the number n is less than or equal to 1, it is not prime.
- If n is 2 or 3, it is prime.
- For any number n greater than 3:
- Check divisibility by 2 or 3 (since primes are not divisible by any number other than 1 and themselves).
- For other divisors, test up to sqrt(n), because if n is divisible by any number greater than its square root, it must also be divisible by a smaller number.
- Here’s a basic approach, known as trial division:
Answers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|