Cross Training Home Development Chat Log Concept #2 — Functions & Methods
TNT  ›  Cross Training  ›  Functions & Methods
Concept #2

Functions & Methods

Define logic once. Call it many times.
And understand the crucial difference between a function that returns a value and one that just does something.

What Mama’s Little Function is Really Teaching

Behind the song, the scrolling verses, and the math mnemonic, Mama’s Little Function is demonstrating two different things that a function can be.

The first is f(x) — a function that takes a number, computes a result, and hands that result back to whoever called it. The caller can save it, use it in another calculation, or pass it somewhere else. The value flows outward.

The second is orderedPair — a function that takes two numbers, does something with them (displays a coordinate pair), and then finishes. Nothing comes back. The caller doesn’t save anything from it. The action is the entire purpose.

Every language you’ll encounter has both kinds of functions. The vocabulary changes. The symbols change. The distinction — and the reason it matters — does not.

The concept, before any language

// Define a function that computes and returns a value:
DEFINE f(x):
COMPUTE m·x + b
RETURN the result // sends a value back to the caller

// Define a function that performs an action and returns nothing:
DEFINE orderedPair(x, y):
DISPLAY “(x, y)”
// no RETURN — the action IS the point

// Call them:
y ← f(2)                      // capture the returned value
orderedPair(2, y)           // perform the action — nothing comes back

Every language below is a direct translation of this pseudocode. The concept is identical — only the vocabulary and syntax differ.

The Same Idea in Three Languages

Same functions. Same math. Same call structure. Study what each language makes you say about the types involved — and what it lets you leave unsaid.

JavaScript Vanilla JS (browser)
// Return function: computes a value,
// hands it back to the caller
function f(x) {
    const m = 3.0;   // slope
    const b = 2.0;   // y-intercept
    return m * x + b;
}

// Void-equivalent: performs an action,
// returns undefined implicitly
function orderedPair(x, y) {
    console.log("(" + x + ", " + y + ")");
}

// Entry point — fires when the page loads
window.addEventListener("load", function () {
    const x = 2;
    const y = f(x);       // y = 8 — returned value captured
    console.log(
        "f(" + x + ") = " + y + " ==> "
    );
    orderedPair(x, y);    // action only — undefined discarded
});
Java Java 17+
public class MamasFunctionDemo {

    // Return function: return type (double)
    // is declared in the signature
    public static double f(double x) {
        double m = 3.0;
        double b = 2.0;
        return m * x + b;
    }

    // Void function: void keyword = explicit
    // declaration that nothing is returned
    public static void orderedPair(
            double x, double y) {
        System.out.println(
            "(" + x + ", " + y + ")"
        );
    }

    public static void main(String[] args) {
        double x = 2.0;
        double y = f(x);   // y = 8.0 — captured
        System.out.print(
            "f(" + x + ") = " + y + " ==> "
        );
        orderedPair(x, y); // action — void means
                           // nothing is returned
    }
}
Python 3 Python 3.x
# Return function: def keyword for all functions;
# return hands a value back to the caller
def f(x):
    m = 3.0
    b = 2.0
    return m * x + b

# Void-equivalent: no return statement means
# Python returns None implicitly
def ordered_pair(x, y):
    print(f"({x}, {y})")

def main():
    x = 2.0
    y = f(x)             # y = 8.0 — captured
    print(
        f"f({x}) = {y} ==> ", end=""
    )
    ordered_pair(x, y)   # action — None discarded

if __name__ == "__main__":
    main()

Why Does Each Version Look Different?

The guiding question for every Cross Training page: “What does each version reveal about the language’s personality?”

JavaScript

Types are optional. Everything is implicit. JavaScript never defined a void keyword. Every function that doesn’t explicitly return a value gives back undefined — the language’s way of saying “nothing meaningful here.”

The entry point is the browser’s event loop, not a main(). You don’t call your code — you register it, and the browser calls it for you when the page is ready. That shift from calling to registering is one of the most important mindset changes when moving from Java or Python to front-end work.

TypeScript (which compiles to JavaScript) exists entirely to add types back in. The fact that developers created a whole language to do so tells you something about both JavaScript’s philosophy and its constraints.

Java

The signature is a contract. public static double f(double x) tells you, before you read one line of the body: one decimal goes in, one decimal comes out. No guessing. No documentation required.

The void keyword is explicit and required — Java insists you state out loud that a function returns nothing. The compiler then enforces every return statement in every non-void method. This verbosity is intentional: Java was designed for teams, for production code, for situations where correctness matters more than conciseness. The extra characters are documentation.

public static void main(String[] args) is the entry point: Java always knows exactly where execution begins.

Python 3

One keyword for every function. def handles everything — no return type to declare, no void. A function without a return statement gives back None silently. Python trusts you to know what your function does from its name.

The if __name__ == "__main__": guard is one of Python’s most useful idioms. It lets the same file work both as an importable module (when another program imports f and ordered_pair) and as a standalone script (when you run python MamasFunctionDemo.py directly). Java needs separate files. Python bundles both possibilities in one.

Python 3.5+ supports optional type hints (def f(x: float) -> float:), but they’re not enforced at runtime. Use them when the team benefits; skip them when the name says enough.