In math, we use values like -98.1, 2/3 and 42. In math, we also use expressions like 1 × 3, √16, and 5 - 2. These evaluate to results, and typing any of them in as code produces some answer.
Math also has definitions. These are different from values and expressions, because they do not produce results. Instead, they simply create names for values, so that those names can be re-used to make the Math simpler and more efficient.
Definitions always have both a name and an expression. The name goes on the left and the value-producing expression goes on the right, separated by an equals sign:
x = 4
y = 9 + x
The name is defined to be the result of evaluating the expression. Using the above examples, we get "x is defined to be 4, and y is defined to be 13. Important: there is no "answer" to a definition, and typing in a definition as code will produce no result.
Notice that definitions can refer to previous definitions. In the example above, the definition of y
refers to x
. But x
, on the other hand, cannot refer to y
. Once a value has been defined, it can be used in later expressions.
In WeScheme, these definitions are written a little differently, making it clear that we’re talking about definitions:
Try typing these definitions into the Definitions Area on the left, clicking "Run", and then using them in the Interactions Area on the right.
(define x 4)
(define y (+ 9 x))
Just like in math, definitions in our programming language can only refer to previously-defined values.
Here are a few more value definitions. Feel free to type them in, and make sure you understand them.
(define x (+ 5 1))
(define y (* x 7))
(define food "Pizza!")
(define dot (circle y "solid" "red"))
These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). Bootstrap by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.