Define Assignment In Programming

I come from a C/C++ background, but the ideas should be the same.

Declaration - When a variable is declared, it is telling the compiler to set aside a piece of memory and associate a name (and a variable type) with it. In C/C++ it could look like this:

The compiler sees this and sets aside an address location for x and knows what methods it should use to perform operations on x (different variable types will use different access operations). This way, when the compiler runs into the line

It knows to put the integer value 8 (not the floating point value 8) into the memory location also known as 'x'.

Assignment - This is when you stuff a value into the previously declared variable. Assignment is associated with the 'equals sign'. In the previous example, the variable 'x' was assigned the value 8.

Initialization - This is when a variable is preset with a value. There is no guarantee that a variable will every be set to some default value during variable declaration (unless you explicitly make it so). It can be argued that initialization is the first assignment of a variable, but this isn't entirely true, as I will explain shortly. A typical initialization is a blend of the variable declaration with an assignment as follows:

The distinction between initialization and assignment becomes more important when dealing with constants, such as this...

When dealing with constants, you only get to assign their value at the time of declaration/initialization. Otherwise, they can't be touched. This is because constants are often located in program memory vs data memory, and their actual assignment is occurring at compile time vs run time.

Variable Assignment

The '=' symbol is the assignment operator. Warning, while the assignment operator looks like the traditional mathematical equals sign, this is NOT the case. The equals operator is '=='

Design Pattern

To evaluate an assignment statement:

  1. Evaluate the "right side" of the expression (to the right of the equal sign).
  2. Once everything is figured out, place the computed value into the variables bucket.

More info

We've already seen many examples of assignment. Assignment means: "storing a value (of a particular type) under a variable name". Think of each assignment as copying the value of the righthand side of the expression into a "bucket" associated with the left hand side name!

Read this as, the variable called "name" is "assigned" the value computed by the expression to the right of the assignment operator ('=');

Now that you have seen some variables being assigned, tell me what the following code means?

The answer to above questions: the assignment means that lkjasdlfjlskdfjlksjdflkj is a variable (a really badly named one), but a variable none-the-less. jlkajdsf and lkjsdflkjsdf must also be variables. The sum of the two numbers held in jlkajdsf and lkjsdflkjsdf is stored in the variable lkjasdlfjlskdfjlksjdflkj.

Examples of builtin Data and Variables (and Constants)

For more info, use the "help" command: (e.g., help realmin);

Examples of using Data and Variable



The assignment pattern creates a new variable, if this is the first time we have seen the "name", or, updates the variable to a new value!

Read the following code in English as: First, compute the value of the thing to the right of the assignment operator (the =). then store the computed value under the given name, destroying anything that was there before.

Or more concisely: assign the variable "name" the value computed by "right_hand_expression"

Back to Topics List


Leave a Reply

Your email address will not be published. Required fields are marked *