Code 101: Variables

Written by: Reinder de Vries, August 16 2015, in 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.

Join 11.000+ app developers and marketers
  • Get a weekly curated list of app development tools, articles and resources in your inbox
  • 10x your app installs with relevant App Store Optimization and app marketing strategies
  • BONUS: Grab a free copy of the App Toolbox 2017 to supercharge your next app project
Yes, Sign Me Up!

Most Popular Content

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.

Got a killer app idea?

Grab the App Toolbox 2017 to learn how to save time building your app,
and how to 10x your app installs in the App Store. With the toolbox, you'll immediately know how to move forward to build better, more profitable apps.

Get The App Toolbox

Comments & Thoughts

Leave a Reply

Markdown is supported (Learn more). Your email address will not be published. Required fields are marked with *

On The Blog

  • Case Study: Building A Compelling App Landing Page
    Case Study: Building A Compelling App Landing Page

    September 23 2015, in Business, Marketing

    You’ve made your first app, it’s in the App Store and you’ve built an app landing page for it. Business isn’t booming just yet, but you’ve made a compelling roadmap for the coming months: features that will wow your users. Right?
    Learn more...
  • Grand Central Dispatch: Multi-Threading With Swift 3
    Grand Central Dispatch: Multi-Threading With Swift 3

    January 9 2017, in Development

    Multithreading is a mechanism computers can't live without. You know why? CPUs are kinda dumb – they can only do one thing at a time! Fortunately, as an app developer you can use Grand Central Dispatch to make your app execute multiple tasks concurrently. How?
    Learn more...
  • Finally, A True App Discovery Platform Is Here: Pinterest Joins Forces With Apple
    Finally, A True App Discovery Platform Is Here: Pinterest Joins Forces With Apple

    February 12 2015, in Marketing

    This week, Pinterest announced that it will join forces with Apple to give users a greater app discovery tool. Right from Pinterest, you can install apps on your iPhone or iPad, and from within the App Store you can pin your favorite apps. What does that mean for app publishers?
    Learn more...
  • Using In-App Deeplinking To Promote Native App Content
    Using In-App Deeplinking To Promote Native App Content

    April 19 2015, in Marketing

    In a sense, the app industry is still in its nineties. The Apple App Store is a rich-media example of that first web directory from Yahoo!’s Jerry and David: it’s a huge list of apps. Apart from text, images and catalog-style links there’s not much contextual information available. Each day about 300 apps get published in the iOS App Store alone, but most of them never even get close to the Top 100 list. Many perfectly good apps go by largely unnoticed, due to the nature of the app stores.
    Learn more...