Creating and Naming Variables

Creating variables means claim a piece of computer memory to use it while running, give it a name, and you can refer to that information and use it in your source code.

There are three things you have to think about creating variables :

  • Name
  • Value
  • Type

For example if we want to keep score information, we named it 'score'. The value maybe start with 0, and that value can changed over time, or can be vary. And last, what kind of type we want to put in our variable. In this case, 'score' is number. Programming language is care deeply about type of data, because it's will affect how much memory needed to store that data.

To using variable, we have to declare it first. Declaring variable is different between languages. Here are few examples.

Swift :

var score : int

C++ :

int score;

Visual Basic :

Dim score As Integer

Word 'score' is programmer choice, you can change it whatever you like. This called identifier. There are few tips to choosing the right identifier to your variable :

  • You can't use word the language already owns (int, var, true, etc)
  • Make it as clear as possible (just because you can name it anything, doesn't mean you should)
  • In some language, you can't use space. If you want to write more than a word, use underscore (_) or make it to one word. (Example : 'player_score', playerScore)

Never write your identifier like this :

int CuRRent_Sc0_rE

Example above technically is allowed, but never do that. Make simple, short, and clear as possible.

results matching ""

    No results matching ""