What is a variable in programming?

657 views

[ad_1]

What is a variable in programming?

In: Other
[ad_2]

A variable is like a blank in a sentence. The cat sat on a _______. The answer could be anything right? Mat? Carpet? Couch? Etc… A variable can help swap in a value into the sentence or program that helps change the meaning when required.

A variable in programming is like a storage container. You give the storage container a name and put information in that container that you can reuse anytime simply by using the variable name.

A container to store a type of data

Ex int a = 1;

A is a variable of the integer type and the value is 1.

Given you’re into superhero movies, you can imagine a variable like a shapeshifter. A supernatural being that can take many forms and shapes.

However, variables are a bit more boring

Whilst a shapeshifter may transform from a human into a crocodile or some alien species, a variable will transform from the number 1 into the number 13 or similar.

You need this as from a programming perspective you do not always know what shape/value you may need, as it may be reliant on user input. So it is handy ti have something which can take on all sorts of shapes/values.

When software developers write programs we put things in “boxes” – we give the box a name so we know how to find it (or “call” out for it later when we need it) amongst all the other boxes… we can then put what we want into the box and come back to it later and see it again, add something to the box, take something out, empty the box) – the contents can vary so we call it a variable.

A variable is basically a name of an information.

So for example

num = 4

would (in python) declare the variable “num” to have the value 4. So you could do stuff with that like:

eight = num + num

where each “num” has the value 4 and so the result has the value 8. This can also be used to declare stuff in advance for example:

x = input()
y = input()
result = x+y

as the time of writing this you don’t know what value “x” or “y” will have as they are getting their value from the user input but whatever the user inputs, the program will try to add the 2 together.

A variable is a piece of information that the program needs, but may change.

For example, let’s take a calculator program, you type 2 + 2 and get “4.” The 4 is stored as a variable so it knows what to display. From here you can type “x 3,” but until you hit “=” all it knows so far is you want to multiply by 3, so it stores that as another variable. Hit “=” and now the number on screen says 12, you changed that variable that said 4 before to now say 12.

Or we could look at a more obvious use of changing data by looking at games. The way you’re facing, your health and ammo, everything that is able to change is probably stored as a variable.

A variable is a place to store information.

Think of it like a single file folder inside a filing cabinet. You label the folder so you can find whatever’s inside easily, right?

Well, when you’re writing a program, there’s gonna be information you need the program to be able to access easily, whether it’s information you build into the code, or information the end user inputs.

The computer needs a way to store that information and be able to retrieve it on demand; a variable is what we use to allow the computer to do that. They’re called “variables” because the information they store can change.

There are several different kinds of variables (integer, string, et cetera) because the program may need to store, access, and use different kinds of information (words, whole numbers, numbers with decimal places, et cetera). Each type has certain rules that tell the program what kind of data is stored in the variable, and what kinds of things the program can do with that information.

a variable is just a space in memory you have assigned to be able to take and store a value. you assign it it a type and name so that the CPU knows that it should reserve a set amount of memory for it.

its called a variable because outside of very specific use cases the value it contains is subject to change.

a variable is also how a program can store the data it manipulates thru a function

Some if the answers seem to be too complicated so:

It can be whatever you want it to be, a number, a letter, a word, a phrase, etc. You just specify what it is and give it a name so the computer can identify it.

I struggled with this when I started learning programming as well.

A Variable is something in your program that can be changed, like the score in a game, or the amount of money in a bank account.

So you create a variable by writing a name that represents the thing that’s going to be changeable, like SCORE.

SCORE is the name of a variable.

If this is still confusing, DM me and I can talk you through it. Once you get it, it makes sense, but until then it’s just abstract and more confusing that it really needs to be.