Return Values in Programming- Definition and Use

What Are Return Values?

A return value is what a function sends back to whoever called it. You write a function to do work. When that work is done, the function can hand you a result. That's the return value.

Think of it like a vending machine. You put in code (drop your money), the machine runs its logic (dispenses your snack), and it gives you something back (the return value). Without a return statement, most functions just give you undefined or None.

How Return Values Actually Work

When a function executes, it can pass data back using the return keyword. Once a return statement runs, the function stops executing. Everything after the return gets ignored.

Here's the basic pattern:

Return Values in JavaScript

JavaScript functions always return something. If you don't explicitly return, you get undefined.

function add(a, b) {
  return a + b;
}

const result = add(5, 3);
console.log(result); // 8

Return Values in Python

Python works the same way. No explicit return? You get None.

def multiply(x, y):
    return x * y

product = multiply(4, 7)
print(product)  # 28

Return Values in C++

C++ requires you to declare the return type upfront. If your function says it returns an int, it better return an int.

int square(int n) {
    return n * n;
}

Why Return Values Exist

Return values let functions communicate with the outside world. Without them, you'd have no way to get data out of a function once it finishes running.

You need return values because:

Common Return Value Patterns

Returning Multiple Values

Python lets you return multiple values as a tuple. JavaScript handles this differently—you'd return an object or array.

# Python
def get_stats(numbers):
    return min(numbers), max(numbers)

low, high = get_stats([1, 2, 3, 4, 5])

Early Returns

Sometimes you want to exit a function before reaching the end. Early returns are useful for error handling or validation.

function processUser(user) {
    if (!user) return null;
    if (!user.email) return null;
    
    // main logic here
    return processUserData(user);
}

Returning Booleans

Functions that check conditions typically return true or false. This makes code readable.

function isAdult(age) {
    return age >= 18;
}

Return Values vs Side Effects

Some functions don't return anything useful. They just do things—print to the screen, modify a global variable, write to a file. These have side effects.

Pure functions return values without causing side effects. They're easier to test and predict. This doesn't mean side effects are bad. Sometimes you need them. Just know the difference.

How to Use Return Values: A Practical Guide

Here's how to actually work with return values in your code:

Step 1: Write a Function That Returns Something

function calculateTax(price, rate) {
    return price * (rate / 100);
}

Step 2: Capture the Return Value

const tax = calculateTax(100, 20);
console.log(tax); // 20

Step 3: Use the Value in Another Function

function displayTotal(price) {
    const tax = calculateTax(price, 20);
    const total = price + tax;
    console.log(`Total: ${total}`);
}

Step 4: Chain Return Values

const final = calculateTax(calculateTax(100, 10), 5);
// First calculates 10% tax, then applies 5% to that result

Return Value Handling Across Languages

Here's how different languages handle return values:

Language Default Return Syntax Multiple Returns
JavaScript undefined return value; Object or array
Python None return value Tuple (native)
Java N/A (void exists) return value; Object or array
C++ N/A (void exists) return value; Struct or tuple
Ruby nil return value Array

Common Mistakes to Avoid

The Bottom Line

Return values are how functions talk back to you. Learn to use them properly and your code becomes cleaner, more testable, and easier to debug. Every function should either return something useful or clearly do nothing—don't leave callers guessing what they'll get.