Code 101: Variables

Written by: Reinder de Vries, August 16 2015, in App Development

OK, let’s talk about variables for a second. Fundamentally, programming code is made up of 2 building blocks: variables and functions. Just like a natural spoken language is made up of nouns and verbs, things and actions, programming is made up of variables and functions.

In this episode of Code 101 we’ll learn a thing or two about variables. Let’s go!

What’s A Variable?

Let’s look at what a variable is first, before going any further. If we were to consult Wikipedia on the matter, it’d tell us this:

In computer programming, a variable or scalar is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value; this separation of name and content allows the name to be used independently of the exact information it represents.

Whaat? I thought programming was easy and fun!? Well, it is, were it not that Wikipedia doesn’t know that. This is what Wikipedia actually meant:

  1. A variable is information stored in a computer’s memory, like a name, an address and an image.
  2. It consists of two things: a name and the actual data. When you work with digital photographs, they all have a filename. That name is different from the color bitmap data of the photo itself. Such a filename is a reference, and so is a variable a reference to data in computer memory. Like a warehouse, of sorts.
  3. To us humans, variables have readable names, like bob or alice. To a computer, the variable name doesn’t have meaning: it’s a memory address. Such an address can look like this: 0x0FBA33C2. The computer knows what it is, but we don’t. That’s why the compuer maps the human-readable variable names with the computer-readable ones.
  4. Variables are variable, so they can change their value over time. In coding, you often change the value of variables (what’s “inside” the variable is called a value) to create a meaningful program.

Computer Memory Is Like A Warehouse

You could compare computer memory with a warehouse filled with boxes. Each of the boxes have a name, the variable name. Those boxes are stored in alleys, consisting of racks. You could create an address out of those alleys and racks, like: alley 9, rack 4.

When you’re coding, you don’t want to tell the computer to get variable A from alley 9, rack 4. Instead, you work with easy-to-remember human-readable variable names. The computer keeps track of which variable names go where, and matches them with computer-readable address names.

Then, there’s boxes of different sizes and types. Each variable has a type, which defines what kind of variable it is. Some examples:

  • Textual data goes by the type called String.
  • Integer numbers (whole numbers) are called Int.
  • Numbers with a comma, floating-point numbers, are called Float and Double.
  • Boolean values, a particular type of logical values, are stored as Boolean.

Variables can also have a type that’s called a class. That’ll be something we touch on in a later episode of Code 101.

How Do You Use Variables?

It’s fairly simple: you can use a variable if you first declare it, and then initialize it. Declaring is telling the iPhone you’re going to use this variable, with this name, and of this type. Initializing means you’re giving the variable an initial value. You can of course leave it empty, too.

An example:

var name:String = "Steve";

What did we just do?

  • First, we typed the keyword var. It’s an indicator for the computer that we’re about to declare a variable.
  • Then, we tell it the variable name: name.
  • Then, we type a colon and a variable type: :String.
  • Then, we type the assign character. It says: “this is that”, or “name is Steve”. It’s a bit different from maths, where = means equals. In programming, equals is ==, and = means assign.
  • Finally, we type the value of the variable "Steve". It’s a string, and all strings are wrapped by double quotes.

That’s it! But… it doesn’t do anything yet, right?

How Do You Use Variables?

Well, you know functions by now, right? There’s this function, it prints out a value to the Xcode Console. When writing Swift code, you can use the println to print out a line to the console and inspect its contents.

Like this:

// Output: Steve

Of course, that’s pretty boring. Instead, you write this:

function sqrt(a:Int)  
    var b:Int = sqrt(a);



Can you figure out what this function does?

˙Ɛ sᴉ ɥɔᴉɥʍ ’6 ɟo ʇooɹ ǝɹɐnbs ǝɥʇ ʇno sʇuᴉɹd ʇI

That’s it! Now you know how to use variables in your programming endeavours.

Reinder de Vries

Reinder de Vries is a professional iOS developer. He teaches app developers how to build their own apps at Since 2009 he has developed a few dozen apps for iOS, worked for global brands and lead development at several startups. When he’s not coding, he enjoys strong espresso and traveling.

Comments & Questions

Got a comment or question? Let me know! I read and respond to every one. Thanks!