Variables: Difference between revisions

2,148 bytes added ,  15 days ago
PascalABC.NET
(PascalABC.NET)
 
(6 intermediate revisions by 4 users not shown)
Line 1,819:
a[] = [ 2.1 3.14 3 ]
#
funcproc ffoo . .
# i is local, because it is first used in the function
for i = 1 to len a[]
print a[i]
.
.
foo
call f
#
# string
domain$ = "easylang.onlinedev"
print domain$
#
Line 2,388:
=={{header|J}}==
 
<syntaxhighlight lang="j">val=. 0</syntaxhighlight>
val=. 0
</syntaxhighlight>
 
J has two assignment operators. The =. operator declares, initializes, assigns, etc. a local variable. The =: operator does the same for a "global" variable.
 
<syntaxhighlight lang="j">fun =: 3 :0
fun =: 3 :0
val1 =: 0
val1 =. 2
Line 2,403 ⟶ 2,406:
0
val2
|value error</syntaxhighlight>
</syntaxhighlight>
 
Note that the language forbids assigning a "global" value in a context where the name has a local definition.
 
<syntaxhighlight lang="j">fun1 =: 3 :0
fun1 =: 3 :0
val3=. 0
val3=: 0
)
fun1''
|domain error</syntaxhighlight>
</syntaxhighlight>
 
But the purpose of this rule is to help people catch mistakes. If you have reason to do this, you can easily set up another execution context.
 
<syntaxhighlight lang="j">fun2 =: 3 :0
fun2 =: 3 :0
val4=. 0
3 :'val4=:y' y
)
fun2 ''</syntaxhighlight>
</syntaxhighlight>
 
Variables are referred to by name, and exist in locales (which may be used as classes, closures or other stateful references).
Line 3,667 ⟶ 3,675:
=={{header|Pascal}}==
See [[Variables#Delphi | Delphi]]
 
=={{header|PascalABC.NET}}==
Definition of multiple variables with explicit type:
<syntaxhighlight lang="delphi">
var i,j: integer;
</syntaxhighlight>
 
Definition of a variable with initialization:
<syntaxhighlight lang="delphi">
var n: integer := 5;
</syntaxhighlight>
 
Definition of a variable with initialization and type inference:
<syntaxhighlight lang="delphi">
var n := 5;
</syntaxhighlight>
 
Definition of multiple variables with type inference:
<syntaxhighlight lang="delphi">
var (m,x) := (5,4.3);
</syntaxhighlight>
 
Local and global variables with same names:
<syntaxhighlight lang="delphi">
var i: integer;
 
procedure p;
begin
var i: integer;
end;
</syntaxhighlight>
 
It's impossible to use variables with same names in nested lightweight namespaces:
<syntaxhighlight lang="delphi">
var i: integer;
begin
for var i:=1 to 10 do // Error!
Print(i)
end;
</syntaxhighlight>
 
 
=={{header|Perl}}==
Line 4,615 ⟶ 4,664:
see Msg + nl
</syntaxhighlight>
 
=={{header|RPL}}==
A global variable is declared with the <code>STO</code> instruction after the variable name.
A local variable, which disappears at end of execution, is declared with the <code>→</code> instruction before the variable name, which is idiomatically in lowercase.
in both cases, the variable is initialized with the value at stack level 1, whatever its type.
123 'V' STO
123 → v
A new value can then be assigned with the <code>STO</code> instruction:
"AZ" 'V' STO
"AZ" 'v' STO
A variable can contain any type of data, which can change without need for reinitialization, as shown above.
It is possible to add, substract, multiple, divide... the content of stack level 1 to a variable with specific instructions, resp. <code>STO+, STO-, STO*, STO/</code>...
If the variable contains a list, STO+ will append or prepend the content of stack level 1, according to the order of the arguments:
456 'MYLIST' STO+ <span style="color:grey">@ prepend</span>
'MYLIST' 456 STO+ <span style="color:grey">@ append</span>
<code>STO-</code> and <code>STO/</code> have the same versatility:
123 'V' STO- <span style="color:grey">@ V ← 123 - V</span>
'V' 123 STO- <span style="color:grey">@ V ← V - 123</span>
 
=={{header|Ruby}}==
Line 5,408 ⟶ 5,475:
 
Here are a few examples.
<syntaxhighlight lang="ecmascriptwren">var a // declares the variable 'a' with the default value of 'null'
a = 1 // initializes 'a'
a = "a" // assigns a different value to 'a'
222

edits