Look at substitution, not just one returned value
Referential transparency concerns replacing an expression with its value without changing the observable behavior of the surrounding program. In a small integer language, replacing 2 + 3 with five preserves the outer calculation. Replacing a counter-incrementing call with one of its previous return values also removes a state change, even if the returned number looks correct. Decide what counts as observable before making a substitution argument. This lesson considers returned values, stored state and errors. Other language settings may include input/output and termination behavior. A function’s short spelling or a convenient name such as calculate does not establish this property; the relevant question is what the program can observe when that expression is evaluated or replaced.
Identify the complete inputs
Suppose add takes two integers, reads no other state and changes nothing outside its result. Those premises justify replacing add(2, 3) with five and help explain why a previously computed result can be reused. All conditions affecting the result must be accounted for, however. Time, randomness, configuration files or an outer mutable dictionary are additional dependencies even when they are absent from the written parameter list. Equal argument spellings do not prove equal complete inputs. Passing the same mutable object reference twice also does not mean that its contents remained unchanged between calls. Separate value equality from reference identity and explicit inputs from hidden dependencies before reasoning about memoization or any other replacement of an expression.
Equal return values can conceal different effects
Imagine record always returns zero but increments a stored count on every call. The expression record() + record() returns zero, yet replacing it with the constant zero removes two increments. Stable returned numbers therefore do not justify substitution by themselves. The converse problem occurs with a function that only reads an outer setting: it changes nothing, but its result can change when the setting does. Absence of writes and completeness of fixed inputs are different premises. Effects are not inherently undesirable; applications need logging, storage and communication. The useful design step is to make those responsibilities visible, then avoid applying algebraic substitution rules from pure arithmetic to an operation whose state interactions are part of the required behavior.
Include evaluation count and failure behavior
Before replacing 0 * f() with zero, consider whether evaluating f performs output or raises an error. Removing the call may hide behavior the original program required. The constant-folding experiment consequently retains 0 * (1 / 0) because its evaluator visits both operands. A language with a different evaluation strategy must be analyzed under its own rules. Short-circuiting and delayed evaluation can change which expressions are evaluated at all, so familiar operator spellings are not enough to transfer a substitution argument. When discussing an optimization, state which observations must remain unchanged and which evaluations were retained to meet that requirement. A faster timing result alone does not establish that the transformation preserves the program’s meaning.
Make state an explicit input and output
Instead of hiding a counter outside a function, pass the current count explicitly and return both a result and a new count. For input three, a teaching transition might return result zero and count four. The call now exposes which state the calculation used. This does not automatically solve persistence or concurrency: another operation still has to commit that new state somewhere. The purpose of this design is to make the boundary between value calculation and state update explainable, not to claim that real programs can dispense with state. Tests can then distinguish a deterministic calculation of a proposed transition from the external action that applies it, helping locate a failure without conflating the two responsibilities.
Exercise: enumerate observations before substituting
Start the record example with count zero. After record() + record(), the returned value is zero and the count is two. After substituting the constant zero, the returned value is still zero but the count remains zero. The numeric observations agree while the state observations do not. Now explain substituting five for an integer-only add(2, 3) under the stated premises of no hidden reads, writes or failures. Finally consider a pricing function that reads an outer discount rate. Caching its result using only the written price argument is not justified if that rate can change. Name the missing dependency explicitly. Doing so is a stronger understanding than repeating a slogan about identical inputs and outputs without defining what those inputs include.