algebra25 min

Introduction to Functions

Functions as input-output machines, domain, range, and function notation

0/9Not Started

Why This Matters

A function is a rule that takes an input and produces exactly one output. When you write f(x) = 2x + 3, you are giving a name (f) to an expression (2x + 3) and declaring that x is the input. Feed in 4, and you get f(4) = 11. Feed in -1, and you get f(-1) = 1. Functions package computation into reusable, named units -- a concept that maps directly to functions in programming.

The domain is the set of all valid inputs, and the range is the set of all possible outputs. For f(x) = 1/x, the domain is all real numbers except zero (because division by zero is undefined). For g(x) = sqrt(x), the domain is x at least 0 (because you cannot take the square root of a negative in the reals). Understanding domain and range means understanding the limits of what a function can do.

Function notation like f(x), g(t), or h(n) is the standard way to name and communicate about functions. The notation f(3) means "evaluate f at input 3." You can compose functions: g(f(x)) means apply f first, then feed the result into g. This is the mathematical version of piping or chaining in code.

Define Terms

Visual Model

Input xA value from the domain
Rule ff(x) = 2x + 3
Output f(x)A value in the range
DomainAll valid inputs
RangeAll possible outputs
Composition g(f(x))Chain two functions
Validity CheckIs input in domain?

The full process at a glance. Click Start tour to walk through each step.

A function is an input-output machine. Domain controls what goes in; range describes what comes out.

Code Example

Code
// Define and use functions
function f(x) { return 2 * x + 3; }
function g(x) { return x * x; }

console.log("f(4):", f(4));   // 11
console.log("f(-1):", f(-1)); // 1
console.log("g(3):", g(3));   // 9
console.log("g(-3):", g(-3)); // 9

// Function composition: g(f(x))
console.log("g(f(2)):", g(f(2))); // g(7) = 49
console.log("f(g(2)):", f(g(2))); // f(4) = 11
// Note: g(f(x)) is NOT the same as f(g(x))

// Check if pairs represent a valid function
// (no x maps to two different y values)
function isValidFunction(pairs) {
  const seen = new Map();
  for (const [x, y] of pairs) {
    if (seen.has(x) && seen.get(x) !== y) {
      console.log(`Not a function: x=${x} maps to ${seen.get(x)} and ${y}`);
      return false;
    }
    seen.set(x, y);
  }
  console.log("Valid function");
  return true;
}

isValidFunction([[1, 2], [2, 4], [3, 6]]);       // Valid
isValidFunction([[1, 2], [1, 3], [2, 4]]);       // Not valid

// Domain check
function safeDivide(x) {
  if (x === 0) return "undefined (not in domain)";
  return 1 / x;
}
console.log("\n1/x at x=2:", safeDivide(2));   // 0.5
console.log("1/x at x=0:", safeDivide(0));     // undefined

Interactive Experiment

Try these exercises:

  • Define h(x) = x^2 - 4x + 3. Evaluate h(0), h(1), h(2), h(3). At which inputs does h equal 0?
  • Is the set of pairs (1,2), (2,3), (3,2) a function? What about the set (1,2), (1,3)? Why?
  • What is the domain of f(x) = sqrt(x - 5)? For which values of x is the input to sqrt non-negative?
  • Compose f(x) = x + 1 and g(x) = 3x. Compute f(g(2)) and g(f(2)). Are they equal?
  • Create a function with domain "all integers from 1 to 10" and range "only even numbers." What rule works?

Quick Quiz

Coding Challenge

Valid Function Checker

Write a function called `isFunction` that takes an array of [x, y] pairs and returns true if the pairs represent a valid mathematical function (no x value maps to two different y values), and false otherwise. Note: the same x mapping to the same y twice is fine -- it is NOT a duplicate conflict.

Loading editor...

Real-World Usage

Functions are the most fundamental concept bridging math and programming:

  • Programming: Every function in code (def, function, lambda) is a mathematical function that maps inputs to outputs.
  • Spreadsheets: Functions like SUM, AVERAGE, and VLOOKUP take cell ranges as inputs and produce computed outputs.
  • APIs: A REST endpoint is a function: send a request (input), receive a response (output).
  • Database queries: A SQL query is a function from a database state to a result set.
  • Machine learning: A trained model is a function that maps feature vectors to predictions.

Connections