liveBook · Manning

Variable

As explained in Julia as a Second Language

A variable is a named binding that associates an identifier with a value or other computation during program execution.

A variable gives a value a name so you can reuse it in a calculation instead of repeatedly typing the value. In Julia, write `y = 7` to bind the identifier `y` to `7`. Then `z = 3` and `y + z + 2` produce `12`. section 2.2

The `=` operator binds a name to a value; `==` compares expressions for equality. A variable can be rebound: after `x = 2`, evaluating `x = x + 3` leaves `x` bound to `5`. Think of this as moving a name from the old result to the new one, rather than changing a number inside a fixed slot. section 2.2.1 section 2.7

Figure 2.2 Binding variable x to different memory locations
02-02

The diagram follows the `x` label as it moves from the original value to the result of `x + 3`. section 2.2.1

A constant also gives a value a name, but unlike a variable, it is not meant to be changed; declare one with `const`. Identifiers are case-sensitive, so `foo` and `Foo` are different names. Reserved words such as `if`, `while`, and `const` cannot be variable names. section 2.2

How Julia as a Second Language covers it

How deep it goes
Works through binding and rebinding with a memory-label analogy, contrasting them with fixed-slot assignment in C/C++.
Examples
Uses Julia REPL calculations with `y` and `z`, then rebinds `x` from `2` to `5`.
What it assumes
Starts with basic numbers and calculations, introducing identifiers and binding through short Julia REPL examples.
What you can do after
Practice/perform naming values, reusing them in arithmetic, and rebinding variables in Julia.
How it connects
Connects variables to constants, identifiers, functions, assignment, equality comparison, and dynamically typed languages.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.