Variables: Difference between revisions

2,975 bytes removed ,  3 years ago
Eliminated duplicate BASIC section, minor edits on Commodore BASIC entry.
m (→‎{{Header|Tiny BASIC}}: -fix header level)
(Eliminated duplicate BASIC section, minor edits on Commodore BASIC entry.)
Line 503:
 
In Applesoft BASIC, variables are global and there is no scope. And, it is not an error to reference a variable before it has been assigned.
The LET keyword is optional. Almost all math is done using floating point numbers by default. Using floating point variables is almost always faster than using integer variables which require extra conversion between floating point and integer. Integers use less space than floating point values. Applesoft BASIC array indexes start at zero.
 
<lang ApplesoftBasic> 10 A = 1.7: REM LET IS NOT REQUIRED
20 LET B% = 1.7: REM THE PERCENT SIGN INDICATES AN INTEGER; THIS GETS TRUNCATED DOWN
30 LET C$ = "0121": REM THE DOLLAR SIGN INDICATES A STRING DATA TYPE. THE LEADING ZERO IS NOT TRUNCATED
40 DIM D(20): REM CREATE AN ARRAY OF 21 FLOATING POINT NUMBERS
50 DIM E$(5,10): REM CREATE A TWO DIMENSIONAL ARRAY OF 66 STRINGS
60 LET D(1) = 1.3: REM ASSIGN THE SECOND ELEMENT OF D
70 Y$(3) = "ROSE": REM ASSIGN A VALUE TO THE FOURTH STRING
80 PRINT X: REM UNASSIGNED FLOATING POINT AND INTEGER VARIABLES HAVE A DEFAULT VALUE OF ZERO
90 PRINT Y$(2): REM UNASSIGNED STRING VARIABLES ARE EMPTY
100 PRINT Y$(3);"TTA CODE": REM THERE WON'T BE SPACES BETWEEN ROSE AND ETTA
110 F%(10) = 0: REM IF ARRAYS ARE NOT DECLARED THEY HAVE 11 ELEMENTS BY DEFAULT; IE. DIM F%(10)
120 PRINT G: REM THIS PRINTS 0 AND IS NOT AN ERROR EVEN THOUGH G HAS NOT BEEN DEFINED
130 PRINT D(0): REM THIS IS NOT AN ERROR BECAUSE ELEMENTS ARE NUMBERED FROM ZERO.
140 PRINT F%: REM THIS PRINTS 0 BECAUSE F% IS A DIFFERENT VARIABLE THAN THE ARRAY F%(10)
150 LET D(21) = 6: REM THIS IS AN ERROR BECAUSE D ONLY HAS 21 ELEMENTS INDEXED FROM 0 TO 20.</lang>
 
 
=={{header|BASIC}}==
 
In BASIC, variables are global and there is no scope. However, it is an error to reference a variable before it has been assigned.
 
<lang gwbasic>10 LET A=1.3
20 LET B%=1.3: REM The sigil indicates an integer, so this will be rounded down
30 LET C$="0121": REM The sigil indicates a string data type. the leading zero is not truncated
40 DIM D(10): REM Create an array of 10 digits
50 DIM E$(5.10): REM Create an array of 5 strings, with a maximum length of 10 characters
60 LET D(1)=1.3: REM Assign the first element of d
70 LET E$(3)="ROSE": REM Assign a value to the third string
80 PRINT D(3): REM Unassigned array elements have a default value of zero
90 PRINT E$(3): REM Ten spaces because string arrays are not dynamic
100 PRINT E$(3);"TTA CODE": REM There will be spaces between rose and etta
110 DIM F%(10): REM Integers use less space than floating point values
120 PRINT G: REM This is an error because f has not been defined
130 PRINT D(0): REM This is an error because elements are numbered from one
140 LET D(11)=6: REM This is an error because d only has 10 elements
150 PRINT F%: REM This is an error because we have not provided an element number
160 END</lang>
 
==={{header|Applesoft BASIC}}===
 
In Applesoft BASIC, variables are global and there is no scope. And, it is not an error to reference a variable before it has been assigned.
The LET keyword is optional. Almost all math is done using floating point numbers by default. Using floating point variables is almost always faster than using integer variables which require extra conversion between floating point and integer. Integers use less space than floating point values. Applesoft BASIC array indexes start at zero.
 
<lang ApplesoftBasic> 10 A = 1.7: REM LET IS NOT REQUIRED
Line 571 ⟶ 528:
'''Naming and Data Types'''
 
A variable can be uniquely identified with a maximum of two (2) alpha-numeric characters, and the first must be a letter. The type of variable is identified with either a <code>%</code> or <code>$</code> or no symbol at all. Certain reserved words that are two characters long cannot be used as variables (i.e.g. <code>TO</code>, <code>ON</code>, etc.)
 
{| class="wikitable"
Line 601 ⟶ 558:
<lang gwbasic>1 rem rosetta code
5 rem commodore basic variable demonstration
10 print chr$(147);chr$(14);:ti$="000000":rem see lines 380420-415460
15 rem numeric variables default to 0; strings default to empty
20 print a:print b%:print c$:print
Line 726 ⟶ 683:
Some models allow <code>TI$</code> to be a '''seven digit''' number able to track 1/10 seconds as the final digit. Use the following modifications for those models (e.g. CBM-II):
 
<lang gwbasic>10 print chr$(147);chr$(14);:ti$="0000000":rem see lines 380420-415460
 
430 t$=left$(ti$,2)+":"+mid$(ti$,3,2)+":"+mid$(ti$,5,2)+"."+right$(ti$,1)
113

edits