3. Variables

Hello, friend. Welcome back. Your day is about to get better. You’re about to hang out with variables. In this lesson, We are going to teach you all about your new friends and how they can help you write code.

First Look

Consider the following function:

AddNumbers returns a number
  a = 2
  b = 3
  c = a + b
  return c

What do you think would be returned if you call the AddNumbers function? If you answered 5, congratulations! You’re learning fast! If not, don’t worry, this is hard stuff. Here’s why the return value is 5:

The function signature tells us that we have a function named AddNumbers. It also tells us that it will return a number if we call it. This is what we’ve seen in the previous lesson.

The function body says:

  • assign the number 2 to the letter a
  • assign the number 3 to the letter b
  • assign the result of a + b to the letter c
  • return the value of c (which is 5)

These 3 letters, ab and c are variables and we will explain exactly what they are and why you would use them.

Boxes

Variables are like boxes in which you can store anything you want: numbersdates, bits of text called strings (we’ll talk about strings later. They’re super fun). To continue with the box analogy, the variable name would be the label on the box.

Imagine that you store these boxes on a shelf. The shelf would be the part of your computer’s memory known as the RAM. Each time you need to access something that is in one of the boxes, you would look for the appropriate box by its label, remove it from the shelf and open it to retrieve its content. That’s exactly what your computer is doing when your program requests a value that is inside a variable: it looks within the RAM for the variable name and retrieves the associated value.

Your Variables, Your Names

Say it’s moving day. And you’re packing up your boxes. You’d label them, right? Otherwise you’d end up opening the boxes with all of your kitchen supplies in your bedroom. And that would be a mini disaster, wouldn’t it? So, much like you would label a real moving box, you get to choose which label you attach to your variables. Previously, we used single letters to name the variables such as a. That’s not good programming practice. Instead, you should use words that describe the content of the variable: ColorBirthdateUserIdPhoneNumber, and so on.

Each programming language has a set of rules for what constitutes a valid variable name. Some only allow letters and numbers, others allow special characters.

Each language has its own style guide that stipulates best practice for how variables should be named. Although these are guides, and not enforced by the language itself, they enable different programmers to understand each other’s intent. For example, a style rule for variable naming could be separate multiple words with underscoresSpaceship_ColorPhone_Number.

All Equal Signs Are Not Created Equal

Mathematics taught you that the ‘=‘ sign means equals. Not in programming! We repeat: in programming, ‘=‘ does not mean equals! This is probably the most common mistake novice programmers make, so do what you need to do, to ensure you remember this rule. We can recommend a good tattoo parlor, if required!

So what does = mean, then? We are sure you’ve figured it out already: it means assign the value on the right to the variable on the left. It is not called an equal sign either. It is called the assignment operator. Time to read this paragraph one more time. What does = represent again?

The Purpose Of Variables

But why use variables at all? Why would we assign a and b with 3 and 2? Why not just write 3 and 2 in a function?

By using variables we make our functions flexible. And the most beautiful computer programs are the most flexible ones. Wouldn’t it be cool if we could re-use our AddNumbers function to add any two numbers together, not just 3 + 2? Well, we can! By combining arguments and variables together. Look at this slightly modified AddNumbers function:

AddNumbers(Number1, Number2) returns a number
  a = Number1
  b = Number2
  c = a + b
  return c

What would executing AddNumbers(5,7) return? And AddNumbers(10,20)? If your answers are 12 and 30, you can pat yourself on the back as you really seem to be getting the hang of what arguments and variables are, as well as understanding what we can do with them. Good going!

Arguments Are Variables

You may have noticed that in the previous lesson, the MakeDrink function didn’t use variables. We directly used the DrinkName argument with our conditional statements (the two if lines):

MakeDrink(DrinkName)
  if DrinkName is Coffee then
    press the 'Coffee' button

  if DrinkName is Tea then
    press the 'Tea' button

  Wait for the machine to fill the paper cup
  Take the cup

That’s because arguments are variables themselves. We could therefore simplify the AddNumbers function like this:

AddNumbers(Number1, Number2) returns a number
  c = Number1 + Number2
  return c

…and then simplify it even more by placing the operation directly in the return statement:

AddNumbers(Number1, Number2) returns a number
  return Number1 + Number2

Neat, huh? It may be hard to believe right now, but this is a great chunk of the knowledge you need in order to write awesome apps.

Summary

We’ve now seen what variables are and why we would use them. We know that arguments are variables too. We also know that we can choose the names we want to give our variables and we’re not limited to single letters.

And we bet you haven’t forgotten, but here it is again: the assignment operator is represented by = and it means assign the value on the right to the variable on the left

Important words

  • Variable
  • Assignment operator

Exercise

Find information on the web about what computer memory is. Especially the RAM part. Try to teach it to your fellow students in the comments section below.

Share to friends