1
Functions and Return Values
Myat Ei Zin
University of the People
CS 1101-01 Programming Fundamentals – AY 2025 – T5
Instructor Name: Gbenga Vincent Dania
17th July, 2025
Unit-4
2
Part – 1 : Incremental Development of Hypotenuse Function
As a software developer, I was tasked with writing a Python function called hypotenuse that
calculates the length of the hypotenuse of a right triangle using the Pythagorean theorem:
hypotenuse = sqrt(a^2 + b^2), where a and b are the lengths of the triangle's legs. To ensure clear
structure and ease of debugging, I applied incremental development, breaking down the task into
manageable steps (Downey, 2015).
Stage 1: Define the Function and Return a Placeholder Value
In the first step, I defined the basic structure of the function and returned a fixed value for
testing. This ensured that the function accepts parameters correctly and has no syntax errors
(Downey, 2015).
def hypotenuse(a, b):
return 0
Test Code:
print(hypotenuse(3, 4))
Expected Output:
0
Stage 2: Add the Squaring of Each Leg
In this stage, I computed a² and b² and returned their sum. This test helped verify that the
squaring operation and basic arithmetic were functioning as intended (Downey, 2015).
def hypotenuse(a, b):
square_sum = a**2 + b**2
return square_sum
Test Code:
print(hypotenuse(3, 4)) # 3^2 + 4^2 = 9 + 16 = 25
Expected Output:
25
Unit-4
3
Stage 3: Add Square Root Calculation
In the final stage, I used the math.sqrt() function from the math module to compute the square
root of the sum of squares. This completed the implementation of the hypotenuse function using
the Pythagorean theorem (Downey, 2015).
import math
def hypotenuse(a, b):
square_sum = a**2 + b**2
hypotenuse_length = math.sqrt(square_sum)
return hypotenuse_length
Test Code and Outputs:
print(hypotenuse(3, 4)) # Expected: 5.0
print(hypotenuse(5, 12)) # Expected: 13.0
print(hypotenuse(8, 15)) # Expected: 17.0
Final Outputs:
5.0
13.0
17.0
Summary of Incremental Steps
Stage Action Output (Test Input: 3,4)
1 Return 0 as a placeholder 0
2 Return sum of squares (a² 25
+ b²)
3 Return sqrt(a² + b²) using 5.0
math.sqrt
Conclusion
Unit-4
4
Through incremental development, I ensured that each part of the function worked correctly
before moving on to the next. This method made debugging easier and led to a successful final
implementation (Downey, 2015).
Here is the screen short of my code implementation for part – 1
Part – 2: Incremental Development of a Custom Function
Introduction
As a software developer seeking to build a strong professional portfolio, I aim to demonstrate my
capability to design and implement custom software solutions. This document outlines the
incremental development of a function that calculates the factorial of a given non-negative
integer. This mathematical operation is commonly used in combinatorics, statistics, and
algorithm design, showcasing both logic and programming fundamentals (Downey, 2015).
Stage 1: Define the Function and Return a Placeholder
The first step is to define the structure of the function, returning a fixed value to verify syntax
and parameter handling. This approach aligns with incremental development principles, allowing
for testing at each step (Downey, 2015).
Unit-4
5
def factorial(n):
return 1
Test Code:
print(factorial(5))
Expected Output:
1
Stage 2: Add a Simple Loop to Multiply Numbers
In this stage, I introduced a loop to calculate the factorial iteratively. The function multiplies
each integer from 1 to n, storing the result. This tests the logic for looping and basic arithmetic
(Downey, 2015).
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
Test Code:
print(factorial(5)) # 5! = 120
Expected Output:
120
Stage 3: Add Input Validation
To make the function more robust, I added input validation to ensure that only non-negative
integers are processed. This enhances reliability and user-friendliness (Downey, 2015).
def factorial(n):
Unit-4
6
if not isinstance(n, int) or n < 0:
return "Input must be a non-negative integer."
result = 1
for i in range(1, n + 1):
result *= i
return result
Test Code:
print(factorial(-3)) # Invalid input
print(factorial(0)) # 0! = 1
print(factorial(6)) # 6! = 720
Expected Output:
Input must be a non-negative integer.
1
720
Summary of Incremental Steps
Stage Action Sample Output
1 Return placeholder value 1
2 Add loop for factorial 120
computation
3 Add input validation 1, 720 or error message
Conclusion
Through this step-by-step approach, I developed a reusable and robust function that can compute
factorials while handling errors gracefully. This example demonstrates my ability to apply
logical thinking, write clean code, and follow software development best practices (Downey,
2015). It will serve as a valuable piece in my programming portfolio.
Here is the screen short of my code implementation for part – 2
Unit-4
7
References
Downey, A. (2015). Think Python: How to Think Like a Computer Scientist (2nd ed.). O’Reilly
Media. Retrieved from https://greenteapress.com/wp/think-python-2e/
Unit-4