Code 101: Variables

Written by: Reinder de Vries, August 16 2015, in Code 101, Programming, Q&A

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.

Join 11.000+ app developers and marketers
  • Every Monday: get curated app marketing news, resources, tools and goodies in your inbox
  • Grow your app's userbase with the latest App Store Optimization and app marketing strategies
  • FREE: Get the free step-by-step Get Started With Your App Idea guide to set yourself up for App Store success!
Yes, Sign Me Up!

Popular Posts

Written By: Reinder de Vries

Reinder de Vries is an indie app maker who teaches aspiring app developers and marketers how to build their own apps at He has developed 50+ apps and his code is used by millions of users all over the globe. When he’s not coding, he enjoys strong espresso and traveling.

Get instant access to The Step-by-Step Guide: Get Started With Your App Idea

(PLUS: see the tool we use to save a ton on back-end coding)


Comments & Thoughts

On The Blog