forked from verhas/jScriptBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal.html
More file actions
185 lines (137 loc) · 8.5 KB
/
Copy pathglobal.html
File metadata and controls
185 lines (137 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
| Generated by Apache Maven Doxia at Jan 17, 2013
| Rendered using Apache Maven Fluido Skin 1.2.1
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>jScriptBasic - jScriptBasic Project Documentation</title>
<link rel="stylesheet" href="../css/apache-maven-fluido.min.css" />
<link rel="stylesheet" href="../css/site.css" />
<link rel="stylesheet" href="../css/print.css" media="print" />
<script type="text/javascript" src="../js/apache-maven-fluido.min.js"></script>
<meta name="author" content="Peter Verhas" />
<meta name="Date-Creation-yyyymmdd" content="20120816" />
<meta name="Date-Revision-yyyymmdd" content="20130117" />
<meta http-equiv="Content-Language" content="en" />
</head>
<body class="topBarDisabled">
<div class="container-fluid">
<div id="banner">
<div class="pull-left">
<a href="http://scriptbasic.com" id="bannerLeft">
<img src="../" alt="ScriptBasic for Java"/>
</a>
</div>
<div class="pull-right"> </div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 2013-01-17</li>
<li class="divider">|</li> <li id="projectVersion">Version: 1.0.4-SNAPSHOT</li>
<li class="pull-right"> <a href="http://scriptbasic.com" class="externalLink" title="ScriptBasic">ScriptBasic</a>
</li>
</ul>
</div>
<div class="row-fluid">
<div id="leftColumn" class="span3">
<div class="well sidebar-nav">
<h3>Documentation for SB4J</h3>
<ul>
<li class="none">
<a href="../intro.html" title="Introduction">Introduction</a>
</li>
<li class="none">
<a href="../name.html" title="Name of the game">Name of the game</a>
</li>
<li class="none">
<a href="../design.html" title="History and Design Goals">History and Design Goals</a>
</li>
<li class="none">
<a href="../basic.html" title="Basic Language Implemented">Basic Language Implemented</a>
</li>
<li class="none">
<a href="../install.html" title="Installation">Installation</a>
</li>
<li class="none">
<a href="../advanced/index.html" title="Embedding API">Embedding API</a>
</li>
<li class="none">
<a href="../configure.html" title="Configuration">Configuration</a>
</li>
</ul>
<h3>Project Documentation</h3>
<ul>
<li class="collapsed">
<a href="../project-info.html" title="Project Information">Project Information</a>
</li>
<li class="collapsed">
<a href="../project-reports.html" title="Project Reports">Project Reports</a>
</li>
</ul>
<hr class="divider" />
<div id="poweredBy">
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<div class="g-plusone" data-href="http://www.scriptbasic.com" data-size="tall" ></div>
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
<img class="poweredBy" alt="Built by Maven" src="../images/logos/maven-feather.png" />
</a>
</div>
</div>
</div>
<div id="bodyColumn" class="span9" >
<div class="section"><h2>LOCAL and GLOBAL<a name="LOCAL_and_GLOBAL"></a></h2><p>Variables in ScriptBasic for Java need not be declared. They are created automatically when the program execution sees a variable. When a variable is used in a subroutine, however it can be either a local or a global variable. The interpreter can not derive the intention of the programmer and for this reason there are two commands <tt>LOCAL</tt> and <tt>GLOBAL</tt> that can be used to declare variables.</p><p>Inside a subroutine all variables are local unless defined global.</p><p>A variable can be declared tio be global inside a subroutine or in the main program code. <tt>LOCAL</tt> and <tt>GLOBAL</tt> declarations can be used in subroutines only before the first executable command. In the main program you can use <tt>GLOBAL</tt> anywhere.</p><p>Lets have a look at the following program:</p><div class="source"><pre class="prettyprint">' Program demonstrating global
' and local variables
gVar1 = "global"
gVar2 = "global"
anyVar = "global"
arg = "global"
GLOBAL myLocalVar2
sub mySub(arg)
LOCAL anyVar, myLocalVar
GLOBAL gVar2
anyVar = "local"
arg = "local"
gVar1 = "local"
gVar2 = "local"
myLocalVar1 = "local"
myLocalVar2 = "local"
endsub
mySub "argument"
print "gVar1=",gVar1
print "\n"
print "gVar2=",gVar2
print "\n"
print "anyVal=",anyVar
print "\n"
print "arg=",arg
print "\n"
print "myLocalVar1=",myLocalVar1
print "\n"
print "myLocalVar2=",myLocalVar2
print "\n"
</pre></div><p>The output of the program is</p><!-- 23456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 --><div><pre>gVar1=global
gVar2=local
anyVal=global
arg=global
myLocalVar1=undef
myLocalVar2=undef</pre></div><p>Only the variable <tt>gVar2</tt> was modified to hold the string <tt>"local"</tt> in the subroutine, because that is the only variable declared to be global. All other variables including the argument is local.</p><div class="section"><h3>Practice<a name="Practice"></a></h3><p>Treating all variables local unless declared global is a safe practice. This prevents a subroutine to accidentally overwrite the value of a global variable.</p><p>Even though it is not required to declare every variable, it is a good practice to declare variables either to be global and local. ScriptBasic for Java has an internal feature that makes it possible to programmatically configure the interpreter to alter the behavior above. It can be forced to treat any non declared variable global inside subroutines. Use ScriptBasic for Java only if you really have to because what you do is actually harm your BASIC programmers.</p><p>The other possible programmatic configuration is to treat any non declared variable an error. To make the interpreter usable this way the <tt>GLOBAL</tt> keyword can be used in the main program code and you can declare any local variable as <tt>LOCAL</tt> inside a subroutine. </p></div></div>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row span16">Copyright © 2013
<a href="http://www.verhas.com">Verhas and Verhas Software Craftsmen</a>.
All Rights Reserved.
</div>
</div>
</footer>
</body>
</html>