sicp.io
1.1.3 · Naming a method

A procedure gives a process a name.

Build a useful operation from smaller ones and watch names become parameters when the procedure is applied.

Guiding question

How does a procedure definition turn an expression into a reusable method?

  • Read a procedure definition as parameters plus a body
  • Follow one compound procedure application
  • Distinguish a name from the value bound to it

square names a one-parameter procedure. sum-of-squares then combines two applications of square. The definition records a method. Nothing in its body runs until the procedure is applied.

On application, each parameter is bound to an argument value in a new environment. The body is evaluated with those bindings, producing 25 for inputs 3 and 4.

SICP code122 of 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Execution trace0 / 0 events
    Programs run in the browser with their result and execution trace.
    Expected result

    The first program returns 25 and produces no output.

    Trace focus

    Find the two square applications. They use different x bindings even though both calls evaluate the same procedure body.

    Try it yourself

    Change the program and compare the result.

    Define sum-of-cubes using a cube procedure, then evaluate it for 2 and 3.

    Show hint

    Keep the combining procedure independent from the details of cubing one value.

    Complete this lesson

    0 of 18 lessons complete in this chapter0%