Initiatives
- Wolfram Foundation
- MathWorld
- Computer-Based Math
- A New Kind of Science
- Wolfram Technology for Hackathons
- Student Ambassador Program
- Wolfram for Startups
- Demonstrations Project
- Wolfram Innovator Awards
- Wolfram + Raspberry Pi
- Summer Programs
- More.
Explore the latest version of An Elementary Introduction to the Wolfram Language »
Stephen Wolfram
An Elementary Introduction to the Wolfram Language
Second Edition (2017)
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/img/top-next-arrow.png)
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/img/hamburger.png)
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/img/x-icon.png)
39 | Immediate and Delayed Values |
In immediate assignment, the value is computed immediately when the assignment is done, and is never recomputed. In delayed assignment, the computation of the value is delayed, and is done every time the value is requested.
In immediate assignment ( = ), a random color is immediately generated:
![Click for copyable input](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io_1-i.png.en)
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io-1-o.png.en)
Every time you ask for value , you get the same random color:
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io-2-o.png.en)
In delayed assignment ( := ), no random color is immediately generated:
![Click for copyable input](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io_3-i.png.en)
Each time you ask for value , RandomColor[] is computed, and a new color is generated:
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io-3-o.png.en)
The color will typically be different every time:
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io-4-o.png.en)
It ’ s very common to use := if something isn ’ t ready yet when you ’ re defining a value.
You can make a delayed assignment for circles even though n doesn ’ t yet have a value:
![Click for copyable input](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io_6-i.png.en)
Give n a value:
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io-5-o.png.en)
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io-6-o.png.en)
The idea of delayed assignment is directly analogous to the Delayed construct we discussed for deploying webpages. In delayed assignment we don ’ t compute a value until we need it. Similarly, when we use CloudDeploy with Delayed we don ’ t compute the content of a webpage until someone asks for it.
There ’ s a notion of delayed rules too. x rhs computes rhs immediately. But in a delayed rule x rhs (typed :> ), rhs is instead recomputed every time it ’ s requested.
This is an immediate rule, where a specific value for RandomReal[ ] is immediately computed:
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io-7-o.png.en)
You can replace four x ’ s, but they ’ ll all be the same:
![Click for copyable input](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io_10-i.png.en)
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io-8-o.png.en)
This is a delayed rule, where the computation of RandomReal[] is delayed:
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io-9-o.png.en)
RandomReal[] is computed separately when each x is replaced, giving four different values:
![Click for copyable input](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io_12-i.png.en)
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/Files/39-immediate-and-delayed-values-io-10-o.png.en)
x := value | delayed assignment, evaluated every time x is requested |
x value | delayed rule, evaluated every time x is encountered (typed :>) |
39.1 Replace x in by the same random integer up to 100. »
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/img/x-static-icon.png)
Sample expected output:
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/img/open-exercise-in-cloud.png)
Answer & check your solution
39.2 Replace each x in by a separately chosen random integer up to 100. »
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/img/x-static-icon.png)
Sample expected output:
![](https://www.wolfram.com/language/elementary-introduction/2nd-ed/img/open-exercise-in-cloud.png)
Answer & check your solution
Because you don ’ t want to have to recompute things unless it ’ s necessary. It ’ s more efficient to just compute something once, then use the result over and over again.
How does one read := and :> out loud?
:= is usually just “ colon equals ” , though sometimes “ delayed assignment ” . :> is usually “ colon greater ” , though sometimes “ delayed rule ” .
You ’ ll start an infinite loop that ’ ll eventually get cut off by the system. x= is the same story.
What ’ s the significance of inputs being labeled In[ n ]:= , and outputs Out[ n ]= ?
It indicates that inputs are assigned to In[ n ] and outputs to Out[ n ] . The := for input means the assignment is delayed, so that if you ask for In[ n ] the result will be recomputed.
- In the Wolfram Language, the process of computing results is often called evaluation, because it involves finding values of things.
- The Wolfram Language has many ways of controlling evaluation. An example is the function Hold, which maintains an expression in “ held ” form until it is “ released ” .
- The internal form of x=y is Set[x, y] . x:=y is SetDelayed[x, y] . x y is RuleDelayed[x, y] .