Gambas/Assignments

Equal or not equal
In gambas the = sign is used differently than it is in mathematics. It is used to assign a value to a variable.

When the equal sign shows up in the code, you should always think of it as:
 * It is assigned
 * A place in computer memory is reserved for the variable
 * Fill the memory with this value

Then you will not run into illogical conclusions.

The following code is correct in Gambas. Try it out. What is the result?

Translated to natural language this says:

Assign 5 to the variable a Multiply 4 with the old variable a and assign it to the new variable a.

Maybe the first person who started assignment in computers came from Arabia. Perhaps this is why we have to read the line from the right to left.

By the way: the result is 20.

How does this work in a little program: You need one command button to get the program going. The result is shown in the terminal window. A math teacher would not like these equations, but in Gambas (or in basic in general) it is correct. Of course, you could make things clearer by using two different variables. The line is not an assignment. It is a declaration of a variable as a datatype. In this case a is of the datatype integer.

Theory of the assignment
The following code shows you in general the method of assignment: Variable = Expression Assigns the value of an expression to one of the following elements. :
 * A local variable.
 * A function parameter.
 * A global (class) variable.
 * An array slot.
 * An object public variable.
 * An object property.

Example