LessonsBeginner: First stepsLesson 4

Variables

Give values names, keep them, change them, and choose names that make code easy to read.

  • Beginner
  • Lesson 4 of 83
  • About 8 minutes
  • 7 examples

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.

ExampleRun it in the playground
put "Ada" into tName
put 36 into tAge
put tName && "is" && tAge
Output
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.

ExampleRun it in the playground
put "red" into tColour
put "blue" into tColour
put tColour
put 10 into tScore
put tScore + 5 into tScore
put tScore
Output
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.

ExampleRun it in the playground
put "middle" into tWord
put "start-" before tWord
put "-end" after tWord
put tWord
Output
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 withMeansExample
ta temporary variable, used in one handlertTotal
pa parameter, given to a handlerpName
sa script local, shared by a script's handlerssCount
ga global, shared by every scriptgSettings
ka constantkMaxLives
ca custom property of an objectcScore

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.

ExampleRun it in the playground
put 5 into tApples
put tApples + 1
put tPears
put hello
Output
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.

A mistakeIt stops with an error on line 2
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.

ExampleRun it in the playground
get 6 * 7
put it
get "The answer is" && it
put it
Output
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
ExampleRun it in the playground
put "Grace" into tFirst
put "Hopper" into tLast
put tFirst && tLast into tFull
put "Dr. " before tFull
put tFull
Output
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.