liveBook · Manning

Return value

As explained in Grokking Simplicity

The value produced as the result of a function invocation.

A return value is the explicit output produced by a function. A caller can use it for another calculation, pass it to an action, or choose how to present it. Returning the answer instead of writing directly to the DOM makes a function reusable for interfaces, accounting, and shipping. section 4.5 section 4.7

Use return values to separate calculation from action:

  • Replace implicit inputs with arguments and implicit outputs with return values. This turns an action into a calculation that is easier to organize and reuse. section 4.7 section 5.14
  • With synchronous code, return the computed value and perform the consuming action one level up the call stack. section 15.30
  • With asynchronous code, a normal return happens before the result exists; pass a callback and call it later with the result. section 15.29 section 15.30

Returning an asynchronous total

A synchronous calculation can return its total directly. An asynchronous calculation instead accepts a callback: calc_cart_total(cart, callback) computes the cost and shipping, then calls callback(total). The caller can provide update_total_dom or another consumer, such as accounting. section 15.29 section 15.30

How Grokking Simplicity covers it

How deep it goes
Defines explicit outputs, contrasts synchronous returns with asynchronous callbacks, and stops short of broader asynchronous abstractions.
Examples
Uses shopping-cart totals, DOM updates, shipping calculations, accounting reuse, and a synchronous-versus-asynchronous pet-count example.
What it assumes
Assumes basic functions and asynchronous calls while defining inputs, outputs, implicit effects, and callbacks explicitly.
What you can do after
Apply it to extract calculations, return reusable results, and replace asynchronous returns with callbacks.
How it connects
Connects return values with functions, inputs and outputs, side effects, calculations, actions, callbacks, and asynchronous programming.

Also explained in

Compare how 8 books explain return value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Simplicity. The section links point to the parts of the book it draws on.