Const: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 9: | Line 9: | ||
Multiple constants may be declared on a single line, by separating each constant assignment with a comma. | Multiple constants may be declared on a single line, by separating each constant assignment with a comma. | ||
== Example == | == Example (Basic) == | ||
<pre> | <pre> | ||
Rem Const Example | Rem Const Example | ||
'Const defines constants | 'Const defines constants | ||
Const SHAPE = "Rectangle" | Const SHAPE = "Rectangle" | ||
Const AREA = 51 | Const AREA = 51 | ||
Line 23: | Line 24: | ||
Print SHAPE & " area: " & l & " * " & w &" = " & Area | Print SHAPE & " area: " & l & " * " & w &" = " & Area | ||
END Sub | END Sub | ||
</pre> | |||
== Example (JavaScript) == | |||
<pre> | |||
//Const Example | |||
//Const defines constants | |||
var SHAPE = "Rectangle"; | |||
var AREA = 51; | |||
var LENGTH = 7, WIDTH = 11; | |||
PrintArea(LENGTH, WIDTH); | |||
function PrintArea(l, w) { | |||
var Area; | |||
Area = l * w; | |||
NSB.Print((SHAPE) + (" area: ") + (l) + (" * ") + (w + " = ") + (Area)); | |||
} | |||
</pre> | </pre> | ||
Revision as of 06:18, 1 May 2013
Const name=expression
Description
Const declares constants, which can be used in expressions, in place of literal values. The required component, name, must follow standard variable naming conventions. The required component, expression, is any literal, constant, or combination that includes all arithmetic or logical operators (except IS).
All constants declared inside procedures are available only within the procedure. Const works the same way as Dim, however it is used to identify variables whose value should not be changed.
Multiple constants may be declared on a single line, by separating each constant assignment with a comma.
Example (Basic)
Rem Const Example 'Const defines constants Const SHAPE = "Rectangle" Const AREA = 51 Const LENGTH = 7, WIDTH = 11 PrintArea LENGTH, WIDTH Sub PrintArea(l, w) Dim Area Area = l * w Print SHAPE & " area: " & l & " * " & w &" = " & Area END Sub
Example (JavaScript)
//Const Example //Const defines constants var SHAPE = "Rectangle"; var AREA = 51; var LENGTH = 7, WIDTH = 11; PrintArea(LENGTH, WIDTH); function PrintArea(l, w) { var Area; Area = l * w; NSB.Print((SHAPE) + (" area: ") + (l) + (" * ") + (w + " = ") + (Area)); }
Output
Rectangle area:7*11=77