A named place for a value
A variable is a name that holds a value. You put a value into it, and from then on you use the name wherever you want the value.
put "Ada" into tName
put 36 into tAge
put tName && "is" && tAge
Ada is 36
put ... into keeps the value instead of showing it. Notice that tName has no quotes: it is a name, and xTalk swaps it for the value it holds.
Putting something new into a variable
A variable holds one value at a time. Putting a new value into it replaces the old one. The right-hand side is worked out first, so a variable can even be used to change itself.
put "red" into tColour
put "blue" into tColour
put tColour
put 10 into tScore
put tScore + 5 into tScore
put tScore
blue 15
Adding to the end or the front
put ... after adds to the end of what a variable holds, and put ... before adds to the front. The variable keeps what it had.
put "middle" into tWord
put "start-" before tWord
put "-end" after tWord
put tWord
start-middle-end
Choosing names
A variable's name starts with a letter and has no spaces; it can have letters, digits and underscores. Capitals do not matter to xTalk (tname and tName are the same variable), but they help people read long names.
Many xTalk programmers start every name with a letter that says what kind of variable it is. You will see this in the IDE's own scripts and in these lessons:
| Starts with | Means | Example |
|---|---|---|
t | a temporary variable, used in one handler | tTotal |
p | a parameter, given to a handler | pName |
s | a script local, shared by a script's handlers | sCount |
g | a global, shared by every script | gSettings |
k | a constant | kMaxLives |
c | a custom property of an object | cScore |
It is a convention, not a rule, but it has a real benefit: a name like tLength can never clash with xTalk's own words, such as length.
A name xTalk has never seen
In the playground (and in the IDE, unless you turn on Variable Checking), a variable comes into being the first time you put something into it. But a name you have never put anything into is not a variable at all: xTalk reads it as the word itself, as if it were in quotes. Programmers call this an unquoted literal, and it is a habit HyperCard started.
put 5 into tApples
put tApples + 1
put tPears
put hello
6 tPears hello
That is handy for quick sums in the Message Box, and it is also a classic source of bugs: a misspelt name is not a variable, so it is just a word, and a word in a sum stops the script.
put 5 into tApples
put tAples + 1 -- a typing mistake: tAples is just a word
The variable called it
xTalk has one variable it fills for you: it. Commands like get, ask, answer and read put their answer there. get is a short way of putting a value into it.
get 6 * 7
put it
get "The answer is" && it
put it
42 The answer is 42
Try it yourself
Put your first name into tFirst and your last name into tLast. Then build tFull from them with a space between, add Dr. to its front, and put it.
Show a solution
put "Grace" into tFirst
put "Hopper" into tLast
put tFirst && tLast into tFull
put "Dr. " before tFull
put tFull
Dr. Grace Hopper
Words in this lesson
Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.