# Variable Assignment

Next: Conditionals, Previous: Variable Declaration, Index: Index

In order to assign a value to a variable then the programmer will need to use variable assignment.
[variable]:=[value or variable];
If the variable is an array and you wish to access an element of it, then there are two ways syntactically. The classical way of variable[] is allowed, however, the prefered way is via the # operator - variable#index.

## Communication

Depending where variables are allocated to, assignment may or may not produce communication. It should be noted that communication in this form may ONLY follow the SPMD style. Here we list the most common rules, refer to the specific types for more information.

### Element Types where a:=b

a | b | Result
--- | --- | ---
multiple | multiple | local assignment
single[x] | multiple | local on process x only
multiple | single | MPI Broadcast
single[x] | single[x] | Where x==x local
single[x] | single[q] | Where x != q, P2P communication

## Examples

var i:=4;
var j:array[Int,20,10] :: allocated[row[] :: multiple[]];
((j#2)#4):=834;
Like in variable declaration [link] "i", is assigned to the value 4 and declared to be of type Int. Variable "j" is declared to be a two dimensional array, allocated row major on all processes. The third line of the example will access the specific element in the array. It should be noted that arrays can be allocated row or column major (row is the default.) See the col or row type for more details.
var i:=4;
var m:Int::allocated[single[2]];
var f:Int::allocated[single[1]];
i:=m;
f:=m;
In the second example, the assignment "i:=m;" will generate an MPI Broadcast. Due to "f" and "m" being located on different processes, the assignment "f:=m" will result in P2P communication (MPI Send on process 2 and MPI Recv on process 1.)
Last Modified: August 2008