-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path1446.html
More file actions
228 lines (228 loc) · 20.2 KB
/
Copy path1446.html
File metadata and controls
228 lines (228 loc) · 20.2 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SmallBASIC | UNIT</title>
<meta name="description" content="Declares the source module as a unit. Units are a set of procedures, functions and/or variables that can be used by another program or unit.">
<link rel="canonical" href="1446">
<link rel="keywords" href="UNIT name">
<link rel="stylesheet" href="/css/style.css">
<link rel="icon" type="image/png" href="/images/sb-desktop-32x32.png">
<script src="/clipboard.js"></script>
</head>
<body>
<button onclick="topFunction()" id="BackToTopBtn" title="Go to top">⯅</button>
<script src="/backtotop.js"></script>
<div class="wrapAll clearfix">
<nav class="navigation">
<div class="logo">
<a href="/"><img src='/images/sb-logo.png?v=2' alt="logo"></a>
</div>
<div class="navlinks">
<a href="/pages/download.html">Download</a>
<a href="/pages/news.html">News</a>
<a href="/pages/community.html">Community</a>
<a href="/pages/articles.html">Resources</a>
<a class='active' href="/pages/reference.html">Language Reference</a>
<a href="/pages/guide.html">SmallBASIC Manual</a>
</div>
</nav>
<div class="mainsection">
<div class="tabs clearfix">
<div class="tabsRight">
<a target="_github" href="https://github.com/smallbasic/smallbasic.github.io/blob/master/_build/reference/1446-system-unit.markdown">Edit</a>
<a target="_github" href="https://github.com/smallbasic/smallbasic.github.io/commits/master/_build/reference/1446-system-unit.markdown">History</a>
</div>
</div>
<div class="article">
<h1>UNIT</h1>
<blockquote>UNIT name</blockquote>
<div class="siteSub">
<p>
<a href="/">Home</a> >
<a href="/pages/reference.html">Reference</a> >
<a href="/pages/system.html">System</a>
</p>
</div>
<p>Declares the source module as a unit. Units are a set of procedures,
functions, constants or variables that can be used by another program or
unit.</p>
<ul>
<li>UNIT supports ‘namespace’ (Namespaces allow reuse of same names in
different contexts. e.g. BitLib.Set(x) and StrLib.Set(x) are both using
a function with the same name, “Set”, but in different contexts).</li>
<li>While UNIT can be used as a collection of sub-routines for your own
program, UNIT is particularly useful for creating a general-purpose
library. General purpose libraries can be useful for many programs or
projects, the same way the internal routine “PRINT” is useful for many
programs, and not only for specific one.</li>
</ul>
<p>Use EXPORT to export procedures, functions, constants or variables.
Only exported names can be accessed in the main program.</p>
<h3 id="example-1-simple-unit">Example 1: Simple Unit</h3>
<p>First, an example of the unit. Please save it with the filename
“MyTestUnit.bas”</p>
<div class="sourceCode" id="cb1"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="pp">UNIT</span> MyTestUnit</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="pp">export</span> MyFunc</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="pp">export</span> MySub</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="pp">export</span> MyConst = <span class="dv">3.1415</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="pp">export</span> MyVar = <span class="dv">5</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="kw">func </span>MyFunc(a,b)</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> a + b</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="kw">sub </span>MySub(a,b)</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a> <span class="kw">print</span> <span class="st">"a + b = "</span>; a + b</span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code></pre></div>
<p>Second, an example on how to use the unit.</p>
<div class="sourceCode" id="cb2"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="pp">import</span> MyTestUnit <span class="kw">as</span> u</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>u.MySub(<span class="dv">1</span>,<span class="dv">2</span>)</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> u.MyFunc(<span class="dv">2</span>,<span class="dv">3</span>)</span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> u.MyConst</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a>u.MyVar = u.MyVar + <span class="dv">5</span></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> u.MyVar</span></code></pre></div>
<h3 id="example-2-an-unit-for-using-strings">Example 2: An unit for
using strings</h3>
<p>The UNIT file. Please save as strlib.bas.</p>
<div class="sourceCode" id="cb3"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co">' File: strlib.bas</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="co">' ------------</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="co">' In this demo we are using UNIT to add more useful String commands to</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="co">' SmallBASIC, by carefully creating our own String-Library: strlib.bas</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="co">'</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="co">' This demo includes only two useful functions, Lset() and Rset(); You</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="co">' can add more useful functions, subs, constants - but make sure that</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="co">' they are all String commands (not Array, File, Data, etc).</span></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a><span class="co">'</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a><span class="co">' Write your code carefully and efficiently, because you will use these</span></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a><span class="co">' commands frequently in other projects. And don't forget to debug the</span></span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a><span class="co">' code and to add clear documentation, for you and for others.</span></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a><span class="co">'</span></span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a><span class="co">' See also Home-->Article-->Welcome to SmallBASIC-->Units.</span></span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a><span class="co">' ------------</span></span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a><span class="co">'</span></span>
<span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a><span class="co">' Here we declare that this file is a UNIT file.</span></span>
<span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a><span class="co">' Note: Keep file-name and unit-name the same. That helps the SB to</span></span>
<span id="cb3-20"><a href="#cb3-20" aria-hidden="true" tabindex="-1"></a><span class="co">' automatically recompile the required units when it is needed (i.e.</span></span>
<span id="cb3-21"><a href="#cb3-21" aria-hidden="true" tabindex="-1"></a><span class="co">' to create strlib.sbu file).</span></span>
<span id="cb3-22"><a href="#cb3-22" aria-hidden="true" tabindex="-1"></a><span class="co">' The actual file name must be in lower case for Linux OS.</span></span>
<span id="cb3-23"><a href="#cb3-23" aria-hidden="true" tabindex="-1"></a><span class="pp">Unit</span> strlib <span class="co">' (without .bas extension)</span></span>
<span id="cb3-24"><a href="#cb3-24" aria-hidden="true" tabindex="-1"></a><span class="co">' Here we allow other SmallBASIC files to use some of our library keywords:</span></span>
<span id="cb3-25"><a href="#cb3-25" aria-hidden="true" tabindex="-1"></a><span class="pp">Export</span> Lset, Rset</span>
<span id="cb3-26"><a href="#cb3-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-27"><a href="#cb3-27" aria-hidden="true" tabindex="-1"></a><span class="co">' Left justify string s in buffer of length b.</span></span>
<span id="cb3-28"><a href="#cb3-28" aria-hidden="true" tabindex="-1"></a><span class="co">' example: x = Lset(" SmallBASIC ", 15) ' --> x is " SmallBASIC "</span></span>
<span id="cb3-29"><a href="#cb3-29" aria-hidden="true" tabindex="-1"></a><span class="kw">Func </span>Lset(s, b)</span>
<span id="cb3-30"><a href="#cb3-30" aria-hidden="true" tabindex="-1"></a> <span class="dt">Local</span> l = <span class="fu">Len</span>(s)</span>
<span id="cb3-31"><a href="#cb3-31" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-32"><a href="#cb3-32" aria-hidden="true" tabindex="-1"></a> <span class="kw">If </span>l >= b <span class="kw">Then</span></span>
<span id="cb3-33"><a href="#cb3-33" aria-hidden="true" tabindex="-1"></a> Lset = <span class="fu">Left</span>(s, b)</span>
<span id="cb3-34"><a href="#cb3-34" aria-hidden="true" tabindex="-1"></a> <span class="kw">Else</span></span>
<span id="cb3-35"><a href="#cb3-35" aria-hidden="true" tabindex="-1"></a> Lset = s + <span class="fu">Space</span>(b - l)</span>
<span id="cb3-36"><a href="#cb3-36" aria-hidden="true" tabindex="-1"></a> <span class="kw">Fi</span></span>
<span id="cb3-37"><a href="#cb3-37" aria-hidden="true" tabindex="-1"></a><span class="kw">End</span></span>
<span id="cb3-38"><a href="#cb3-38" aria-hidden="true" tabindex="-1"></a><span class="co">' Right justify string s in buffer of length b.</span></span>
<span id="cb3-39"><a href="#cb3-39" aria-hidden="true" tabindex="-1"></a><span class="co">' example: x = Rset(" SmallBASIC ", 15) ' --> x is " SmallBASIC "</span></span>
<span id="cb3-40"><a href="#cb3-40" aria-hidden="true" tabindex="-1"></a><span class="kw">Func </span>Rset(s, b)</span>
<span id="cb3-41"><a href="#cb3-41" aria-hidden="true" tabindex="-1"></a> <span class="dt">Local</span> l = <span class="fu">Len</span>(s)</span>
<span id="cb3-42"><a href="#cb3-42" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-43"><a href="#cb3-43" aria-hidden="true" tabindex="-1"></a> <span class="kw">If </span>l >= b <span class="kw">Then</span></span>
<span id="cb3-44"><a href="#cb3-44" aria-hidden="true" tabindex="-1"></a> Rset = <span class="fu">Right</span>(s, b)</span>
<span id="cb3-45"><a href="#cb3-45" aria-hidden="true" tabindex="-1"></a> <span class="kw">Else</span></span>
<span id="cb3-46"><a href="#cb3-46" aria-hidden="true" tabindex="-1"></a> Rset = <span class="fu">Space</span>(b - l) + s</span>
<span id="cb3-47"><a href="#cb3-47" aria-hidden="true" tabindex="-1"></a> <span class="kw">Fi</span></span>
<span id="cb3-48"><a href="#cb3-48" aria-hidden="true" tabindex="-1"></a><span class="kw">End</span></span></code></pre></div>
<p>Demo file, demo.bas, which is using strlib.bas above:</p>
<div class="sourceCode" id="cb4"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co">' Here we declare that this file is using another UNIT file.</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="co">' Note: The actual file name must be in lower case for Linux OS.</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="pp">Import</span> strlib <span class="co">' (without .bas extension)</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="co">' Note: To access a member of a UNIT we must use the unit-name, a point</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="co">' and the name of the member, e.g. Strlib.Lset("Hello", 10).</span></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a><span class="co">' demo:</span></span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a><span class="fu">Color</span> <span class="dv">14</span>, <span class="dv">1</span></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a><span class="fu">Locate</span> <span class="dv">4</span>, <span class="dv">30</span></span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a><span class="kw">Print</span> Strlib.Lset(<span class="st">" -->>"</span>, <span class="dv">25</span>)</span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a><span class="fu">Locate</span> <span class="dv">5</span>, <span class="dv">30</span></span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a><span class="kw">Print</span> Strlib.Lset(Strlib.Rset(<span class="st">"This way..."</span>, <span class="dv">18</span>), <span class="dv">25</span>)</span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a><span class="fu">Locate</span> <span class="dv">6</span>, <span class="dv">30</span></span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a><span class="kw">Print</span> Strlib.Rset(<span class="st">"-->> "</span>, <span class="dv">25</span>)</span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a><span class="kw">Pause</span></span></code></pre></div>
<div class="lavenderBox">
<div class="header">Code samples using UNIT</div>
<div class="linklist">
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/4tune.bas">4tune.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/units/base64.bas">base64.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 3/bolmo.bas">bolmo.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/units/crgb.bas">crgb.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/crow.bas">crow.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/dogstar5.bas">dogstar5.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/mathematics/matrix_solve.bas">matrix_solve.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/neolithic.bas">neolithic.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/units/properties.bas">properties.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/rogue trader.bas">rogue trader.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/graphics 3/sierpinski in space.bas">sierpinski in space.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/graphics 4/space.bas">space.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 3/star_field.bas">star_field.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/graphics 1/star_field.bas">star_field.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 1/startrek.bas">startrek.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/games 3/startrek.bas">startrek.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/units/strlib.bas">strlib.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/misc/Peter Graphics/try rainbow v2.bas">try rainbow v2.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/misc/Peter Graphics/try rainbow.bas">try rainbow.bas </a>
<a target="_github" href="https://raw.githubusercontent.com/smallbasic/smallbasic.samples/master/utilities/vigenere_cipher.bas">vigenere_cipher.bas </a>
</div>
</div>
<div class="lavenderBox">
<div class="header">System</div>
<div class="linklist">
<a href="/reference/1439.html">CHAIN </a>
<a href="/reference/1521.html">COMMAND </a>
<a href="/reference/1522.html">CWD </a>
<a href="/reference/806.html">DELAY </a>
<a href="/reference/807.html">ENV </a>
<a href="/reference/815.html">ENV </a>
<a href="/reference/1443.html">EXEC </a>
<a href="/reference/1440.html">EXPORT </a>
<a href="/reference/606.html">FRE </a>
<a href="/reference/1525.html">HOME </a>
<a href="/reference/1441.html">IMPORT </a>
<a href="/reference/1462.html">INCLUDE </a>
<a href="/reference/1736.html">MAXINT </a>
<a href="/reference/1735.html">NIL </a>
<a href="/reference/1442.html">OPTION </a>
<a href="/reference/809.html">PAUSE </a>
<a href="/reference/1524.html">PI </a>
<a href="/reference/817.html">PROGLINE </a>
<a href="/reference/810.html">RANDOMIZE </a>
<a href="/reference/818.html">RUN </a>
<a href="/reference/1523.html">SBVER </a>
<a href="/reference/1734.html">SELF </a>
<a href="/reference/812.html">STKDUMP </a>
<a href="/reference/813.html">TROFF </a>
<a href="/reference/814.html">TRON </a>
<a href="/reference/1446.html"><strong>UNIT</strong> </a>
</div>
</div>
</div>
<div class="pagefooter">
This page was last edited on Tue, 12 Sep 2023 14:32:33 +0200
|
<a href="https://en.wikipedia.org/wiki/Markdown" target="_blank" rel="nofollow">Markdown</a>
processed with
<a href="https://pandoc.org/MANUAL.html#pandocs-markdown" target="_blank" rel="nofollow">pandoc 3.1.12.1</a>
</div>
</div>
</div>
</body>
</html>