Skip to content

Zoedayz/UsingReturn

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Understanding Return Values vs. Printing Output

The Common Beginner Confusion

When learning to program, beginners often struggle with a fundamental concept that affects how testable and reusable their code becomes. This confusion centers around two different approaches to getting results from functions:

Approach 1: Function prints the result directly

def calculate_total(price, tax_rate):
    result = price + (price * tax_rate)
    print(f"Total: ${result}")  # Prints directly to console

Approach 2: Function returns the result, caller prints it

def calculate_total(price, tax_rate):
    return price + (price * tax_rate)  # Returns the value

# Somewhere else in the code:
total = calculate_total(100, 0.08)
print(f"Total: ${total}")  # Caller handles printing

Why This Matters: The Testing Problem

The confusion becomes critical when trying to write unit tests:

Testing the "Print-Only" Function (Difficult)

def test_calculate_total_that_prints():
    # How do you test this? The function just prints!
    # You'd need to capture stdout, which is complex and brittle
    calculate_total(100, 0.08)  # Prints "Total: $108.0"
    # No way to easily verify the calculation is correct

Testing the "Return Value" Function (Easy)

def test_calculate_total_that_returns():
    # Simple and clear!
    result = calculate_total(100, 0.08)
    assert result == 108.0  # Easy to verify the calculation

The Root of the Confusion

Beginners often think these two approaches are equivalent because:

  1. Both show the same output to the user - they see the same text on screen
  2. They don't initially think about testing - unit testing concepts come later
  3. They focus on the immediate result - "it works" seems sufficient
  4. They don't consider reusability - what if you need that calculated value elsewhere?

Why Return Values Are Superior

1. Testability

Functions that return values are trivial to test with assertions. Functions that only print require complex output capture mechanisms.

2. Reusability

A returned value can be used in further calculations, stored in variables, or passed to other functions. A printed value disappears into the console.

3. Separation of Concerns

The calculation logic is separate from the presentation logic. The function does one job well.

4. Composability

You can chain functions together when they return values:

tax = calculate_tax(price, rate)
shipping = calculate_shipping(weight, distance)
total = price + tax + shipping

The Learning Journey

This repository contains examples in both Java and Python that demonstrate:

  • Functions that return values vs. functions that print
  • How to test functions that return values
  • Real-world examples showing why returns are more flexible
  • Common mistakes beginners make
  • Exercises to practice the concepts

Java Version

Located in /java/ - uses traditional Java syntax and JUnit for testing

Python Version

Located in /python/ - uses Python idioms and unittest framework

Getting Started

  1. Read the language-specific README in either /java/ or /python/
  2. Run the example programs to see the difference in action
  3. Look at the test files to understand how return values make testing easy
  4. Try the exercises to practice identifying and fixing these issues

The Key Insight

A function should do one thing well. If a function calculates something, it should return that calculation. If you want to display it, do that as a separate step. This separation makes your code more testable, reusable, and maintainable.

The moment beginners understand this distinction, their code quality improves dramatically because they start thinking about functions as reusable building blocks rather than just sequences of instructions that produce output.

About

Method Print vs Return

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 52.3%
  • Java 47.7%