<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yabasic 3</title>
	<atom:link href="http://yabasic.basicprogramming.org/feed" rel="self" type="application/rss+xml" />
	<link>http://yabasic.basicprogramming.org</link>
	<description>Yet Another BASIC</description>
	<lastBuildDate>Sun, 22 Jan 2012 11:01:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>An update</title>
		<link>http://yabasic.basicprogramming.org/an-update</link>
		<comments>http://yabasic.basicprogramming.org/an-update#comments</comments>
		<pubDate>Sun, 02 Oct 2011 10:58:16 +0000</pubDate>
		<dc:creator>Thomas Larsen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://yabasic.basicprogramming.org/?p=592</guid>
		<description><![CDATA[It&#8217;s high time for an update! Most of the fundamentals have been implemented. Development has been proceeding slowly because I&#8217;ve been wrestling with a challenging problem: how should arrays, structures, and other such data types be passed to&#8212;and returned from&#8212;subroutines &#8230;<p class="read-more"><a href="http://yabasic.basicprogramming.org/an-update">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s high time for an update!</p>
<p>Most of the fundamentals have been implemented. Development has been proceeding slowly because I&#8217;ve been wrestling with a challenging problem: how should arrays, structures, and other such data types be passed to&#8212;and returned from&#8212;subroutines in a clean and consistent way? I think that, ideally, we should be able to write code that looks a bit like this:</p>
<pre>person.name$ = "Foo Barbaz"
person.dob.year = 1983
person.dob.month = 10
person.dob.day = 5

printDetails (person)   // how should the contents of "person" be passed?

sub printDetails (p)
   print p.name$ + " was born on " \
            + str$ (p.dob.year) + "/" \
            + str$ (p.dob.month) + "/" \
            + str$ (p.dob.day) + "."
end sub
</pre>
<p>I think I&#8217;ve finally figured out how to solve this problem in an elegant way. I plan to implement <em>tables</em> (which are pretty much equivalent to associative arrays) that will be able to contain values (real numbers, character strings, and&#8212;importantly&#8212;references to other tables) identified by arbitrary keys (integers and character strings). To ensure that tables can be used as structures, <code>a.b</code> will be treated as syntactic sugar for <code>a("b")</code> and subroutines will be able to take references to tables as arguments (for instance, <code>test (a)</code>).</p>
<p>For maximum speed, I plan to implement tables using a composite structure containing both an <em>array</em> part (for values identified by integer keys within a particular range) and a <em>crit-bit trie</em> part (for values identified by any other keys), but the underlying implementation will be abstracted and hidden at the user level.</p>
]]></content:encoded>
			<wfw:commentRss>http://yabasic.basicprogramming.org/an-update/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Seventh development version</title>
		<link>http://yabasic.basicprogramming.org/seventh-development-version</link>
		<comments>http://yabasic.basicprogramming.org/seventh-development-version#comments</comments>
		<pubDate>Tue, 28 Jun 2011 08:13:06 +0000</pubDate>
		<dc:creator>Thomas Larsen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://yabasic.basicprogramming.org/?p=583</guid>
		<description><![CDATA[I&#8217;ve finally managed to implement subroutines! I expected this task to be the greatest obstacle to development progress, and it didn&#8217;t turn out to be so bad after all&#8212;indeed, structures (and modules) are looking more daunting now. A bit of &#8230;<p class="read-more"><a href="http://yabasic.basicprogramming.org/seventh-development-version">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve finally managed to implement subroutines! I expected this task to be the greatest obstacle to development progress, and it didn&#8217;t turn out to be so bad after all&#8212;indeed, structures (and modules) are looking more daunting now. A bit of polishing still remains: local variables are implemented, but static variables aren&#8217;t yet; subroutines can&#8217;t take either array or scalar <em>references</em> as parameters, nor can they return <em>references</em> to arrays or scalars; and I&#8217;m sure there are plenty of bugs that I haven&#8217;t managed to iron out yet.</p>
<p>If you can, please download (<a href="http://yabasic.basicprogramming.org/wordpress/wp-content/uploads/2011/06/Yabasic-3-experimental7.tar.gz">tar.gz</a>, <a href="http://yabasic.basicprogramming.org/wordpress/wp-content/uploads/2011/06/Yabasic-3-experimental7.zip">zip</a>) and test the seventh development version, and report any bugs that you find! Thanks in advance. <img src='http://yabasic.basicprogramming.org/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>What&#8217;s new?</p>
<ul>
<li>Subroutines:
<pre>hello ("Tom")

sub hello (name$)
   print "Hello, " + name$ + "!"
end sub</pre>
</li>
<li>Local variables:
<pre>a = 1
print a
test ()
print a

sub test ()
   print a
   local a = 2
   print a
end sub</pre>
</li>
<li>A <code>FREE</code> command:
<pre>dim array[100]
array[50] = 1
free array</pre>
</li>
<li>Multiple assignments per line:
<pre>a = 1, b = 2, c = 3</pre>
</li>
<li>And a few other little things.</li>
</ul>
<p>Here are a couple of really rough benchmarks.</p>
<pre>// subroutine calling
for call = 1 to 1000000
   test (1, 2, 3)
   test (call, call + 1, call + 2)
next

sub test (a, b, c)
   return a + b + c
end sub</pre>
<p>(On my machine: 3.965 seconds in Yabasic 2.763, 0.530 seconds in Yabasic 3.)</p>
<pre>// recursive subroutine

test (100000)

sub test (a)
   if a test (a - 1)
end sub</pre>
<p>(On my machine: 0.206 seconds in Yabasic 2.763, 0.022 seconds in Yabasic 3.)</p>
]]></content:encoded>
			<wfw:commentRss>http://yabasic.basicprogramming.org/seventh-development-version/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Yet another development version</title>
		<link>http://yabasic.basicprogramming.org/yet-another-development-version</link>
		<comments>http://yabasic.basicprogramming.org/yet-another-development-version#comments</comments>
		<pubDate>Sun, 29 May 2011 05:43:52 +0000</pubDate>
		<dc:creator>Thomas Larsen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://yabasic.basicprogramming.org/?p=557</guid>
		<description><![CDATA[Here&#8217;s a new development release! If you can, please download it (tar.gz, zip), compile it, and test it. This version features arrays (currently with square brackets, just to ease the process of implementing functions&#8212;don&#8217;t panic!) and various other improvements. It&#8217;s &#8230;<p class="read-more"><a href="http://yabasic.basicprogramming.org/yet-another-development-version">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a new development release! If you can, please download it (<a href="http://yabasic.basicprogramming.org/wordpress/wp-content/uploads/2011/05/Yabasic-3-experimental6.tar.gz">tar.gz</a>, <a href="http://yabasic.basicprogramming.org/wordpress/wp-content/uploads/2011/05/Yabasic-3-experimental6.zip">zip</a>), compile it, and test it. This version features arrays (currently with square brackets, just to ease the process of implementing functions&#8212;don&#8217;t panic!) and various other improvements. It&#8217;s entirely memory-clean, and&#8212;dare I say it&#8212;pretty fast. <img src='http://yabasic.basicprogramming.org/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /><br />
<span id="more-557"></span><br />
Here&#8217;s an example program (<a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway&#8217;s Game of Life</a>):</p>
<pre>// create a grid
width = 40
height = 20
dim grid[width, height]
dim new_grid[width, height]

// "seed" grid randomly ("true" means a live cell, "false" means a dead cell)
for x = 1 to width
   for y = 1 to height
      grid[x, y] = int (ran (2))
   next
next

// game loop: repeat until grid does not change
repeat
   // display grid
   for y = 1 to height
      for x = 1 to width
         if grid[x, y] then
            print "%";
         else
            print ".";
         endif
      next
      print
   next
   print "---"

   // process all grid locations
   for x = 1 to width
      for y = 1 to height
         // calculate how many live cells are around the current grid location
         live_neighbours = 0
         for nx = -1 to 1
            for ny = -1 to 1
               if (x + nx >= 1 and x + nx <= width) and \
                     (y + ny >= 1 and y + ny <= height) then
                  if grid[x + nx, y + ny] then
                     live_neighbours = live_neighbours + 1
                  endif
               endif
            next
         next

         // apply rules
         if grid[x, y] then
            if live_neighbours < 2 then
               new_grid[x, y] = false   // dies: underpopulation
            elseif live_neighbours = 2 or live_neighbours = 3 then
               new_grid[x, y] = true    // stays living
            else
               new_grid[x, y] = false   // dies: overpopulation
            endif
         else
            if live_neighbours = 3 then
               new_grid[x, y] = true    // lives: reproduction
            else
               new_grid[x, y] = false   // stays dead
            endif
         endif
      next
   next

   // copy new grid to grid
   no_change = true
   for x = 1 to width
      for y = 1 to height
         if grid[x, y] <> new_grid[x, y] then
            no_change = false
         endif
         grid[x, y] = new_grid[x, y]
      next
   next
until no_change                 // repeat until grid does not change</pre>
<p>And another (Guess the Number):</p>
<pre>// upper limit for number; lower limit is always 1
maximum_number = 100

// number of attempts the player is allowed to guess the number
maximum_attempts = 7

// game loop: repeat until player wants to quit
repeat
   // select a random number
   number = int (ran (maximum_number)) + 1

   // display game introduction
   print "I'm thinking of a number between 1 and " + \
         str$ (maximum_number) + "."
   print "Try to guess it in " + str$ (maximum_attempts) + " attempts or less."

   // let the player try to guess the number
   for attempt = 1 to maximum_attempts
      // get valid guess from player
      do
         print "Attempt " + str$ (attempt) + " of " + \
               str$ (maximum_attempts) + "."
         input "What is your guess (-1 to give up)? " guess

         if guess = -1 then
            break
         endif

         if guess <> int (guess) or (guess < 1 or guess > maximum_number) then
            print "Invalid guess! Try again."
            continue
         endif

         break
      loop

      // give up?
      if guess = -1 then
         break
      endif

      // correct guess?
      if guess = number then
         break
      endif

      // give hint
      if guess < number then
         print "Your guess was too low."
      elseif guess > number then
         print "Your guess was too high."
      endif
   next

   // did the player win or lose?
   if guess = number then
      print "You won. Congratulations!"
   else
      print "You lost. The number was " + str$ (number) + "."
   endif

   // ask the player if he or she wants to play again
   repeat
      input "Do you want to play again? " answer$
      answer$ = lower$ (left$ (answer$, 1))
   until answer$ = "n" or answer$ = "y"
until answer$ = "n"             // repeat until player wants to quit</pre>
<p>Have fun, and please <strike>tear</strike> test it to pieces! <img src='http://yabasic.basicprogramming.org/wordpress/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://yabasic.basicprogramming.org/yet-another-development-version/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Another development version</title>
		<link>http://yabasic.basicprogramming.org/another-development-version</link>
		<comments>http://yabasic.basicprogramming.org/another-development-version#comments</comments>
		<pubDate>Sun, 15 May 2011 03:45:30 +0000</pubDate>
		<dc:creator>Thomas Larsen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://yabasic.basicprogramming.org/?p=545</guid>
		<description><![CDATA[I&#8217;ve been continuing to experiment with the register-based virtual machine model I told you folks about earlier. Here&#8217;s an updated development version of Yabasic: tar.gz, zip. I repeat, it&#8217;s a development version. (You&#8217;ll get a few compile-time warnings: don&#8217;t worry &#8230;<p class="read-more"><a href="http://yabasic.basicprogramming.org/another-development-version">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been continuing to experiment with the register-based virtual machine model I <a href="http://yabasic.basicprogramming.org/registers-vs-stacks">told you folks about earlier</a>. Here&#8217;s an updated <em>development</em> version of Yabasic: <a href="http://yabasic.basicprogramming.org/wordpress/wp-content/uploads/2011/05/Yabasic-3-experimental5.tar.gz">tar.gz</a>, <a href="http://yabasic.basicprogramming.org/wordpress/wp-content/uploads/2011/05/Yabasic-3-experimental5.zip">zip</a>. I repeat, it&#8217;s a development version. (You&#8217;ll get a few compile-time warnings: don&#8217;t worry about them, everything will be cleaned up in the end.) Things are coming along nicely; last night and this morning, for instance, I implemented IF statements, FOR loops, INPUT, and several functions. The basics are pretty much done&#8212;arrays/structures, subroutines, modules, and a few more complicated functions are all that remain to be completed, really.</p>
<p>I&#8217;m really excited about two things. The first is that this version of Yabasic is completely memory-clean: that means there are no memory leaks, and all memory is freed before the interpreter exits. The second is that this version is remarkably fast in comparison to, say, Yabasic 2.763. For example, the following loop:</p>
<pre>for a = 1 to 10000000
next</pre>
<p>takes 1.5 seconds to execute in Yabasic 2.763 and around 0.4 seconds to execute in this experimental version. That&#8217;s a pretty decent increase in speed, if I do say so myself. <img src='http://yabasic.basicprogramming.org/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  In pretty much every other benchmark that I&#8217;ve done, this version&#8212;with its register-based virtual machine model&#8212;has outperformed Yabasic 2.763 substantially, and I think it&#8217;s looking very promising.</p>
<p>Here&#8217;s a (rather inefficient) program that finds prime numbers:</p>
<pre>limit = 10000

for number = 2 to limit
   prime = true
   for check = 2 to int (sqrt (number))
      if not (number mod check) then
         prime = false
      endif
   next

   if prime then
      print number
   endif
next</pre>
<p>Have fun, and please report any bugs that you find! <img src='http://yabasic.basicprogramming.org/wordpress/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://yabasic.basicprogramming.org/another-development-version/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8220;Registers&#8221; vs. stacks</title>
		<link>http://yabasic.basicprogramming.org/registers-vs-stacks</link>
		<comments>http://yabasic.basicprogramming.org/registers-vs-stacks#comments</comments>
		<pubDate>Mon, 11 Apr 2011 00:21:26 +0000</pubDate>
		<dc:creator>Thomas Larsen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://yabasic.basicprogramming.org/?p=536</guid>
		<description><![CDATA[Many interpreters use an internal stack to keep track of values. For example, print "Hello, " + "world!" would typically be represented internally as cPushString "Hello, " cPushString "world!" cConcatenate cPrint This is pretty self-explanatory: the string &#8220;Hello, &#8221; is &#8230;<p class="read-more"><a href="http://yabasic.basicprogramming.org/registers-vs-stacks">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Many interpreters use an internal stack to keep track of values. For example,</p>
<pre>print "Hello, " + "world!"</pre>
<p>would typically be represented internally as</p>
<pre>cPushString "Hello, "
cPushString "world!"
cConcatenate
cPrint</pre>
<p>This is pretty self-explanatory: the string &#8220;Hello, &#8221; is pushed onto the interpreter&#8217;s stack, followed by the string &#8220;world!&#8221;; then the two strings are popped off the stack, concatenated (joined together), and the result is pushed back onto the stack; and, finally, the value at the top of the stack is output.</p>
<p>This would be all well and good if pushing onto and popping off the stack were cheap operations. And, with a good stack implementation, they <em>are</em> remarkably cheap. But the number of pushes and pops in an average program tends to be high, with the unfortunate consequence that stack overhead is quite noticeable.</p>
<p>There is another less commonly used model that involves using virtual fixed registers to record values internally. Under a register-based implementation, we might re-write the program above as</p>
<pre>cStoreString r1 "Hello, "
cStoreString r2 "world!"
cConcatenate r3 r1 r2
cPrint r3
</pre>
<p>This stores the string &#8220;Hello, &#8221; in register 1, stores the string &#8220;world!&#8221; in register 2, and then concatenates the strings in registers 1 and 2, storing the result in register 3, which is then outputted. In this model, there is no stack overhead, because the interpreter already knows exactly where to store values.</p>
<p>Yesterday I wrote an experimental version of Yabasic&#8212;a <em>very</em> experimental version&#8212;that uses a virtual register machine rather than a virtual stack machine. As an unusual twist, commands <em>are</em> registers, so we have</p>
<pre>1 cConstantString "Hello, "
2 cConstantString "world!"
3 cConcatenate 1 2
4 cPrint 3
</pre>
<p>The results are pretty promising. On my computer,</p>
<pre>a = 0

while (a &lt; 10000000)
   a = a + 1
wend
</pre>
<p>runs in around 0.4 seconds under the new implementation. Under Yabasic 2.9.16, the same program takes around 0.9 seconds to run. Under Yabasic 2.763, the program runs in about 1.1 seconds.</p>
<p>And, you know, it really wasn&#8217;t all that hard to make the register-based version of Yabasic. It&#8217;s still <em>really</em> incomplete at the moment, but so far there&#8217;s been very little of the fiddling I was afraid of.</p>
<p>Check it out: <a href="http://yabasic.basicprogramming.org/wordpress/wp-content/uploads/2011/04/Yabasic-3-experimental2.tar.gz">Yabasic-3-experimental2</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://yabasic.basicprogramming.org/registers-vs-stacks/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Yabasic 3 progress update</title>
		<link>http://yabasic.basicprogramming.org/yabasic-3-progress-update</link>
		<comments>http://yabasic.basicprogramming.org/yabasic-3-progress-update#comments</comments>
		<pubDate>Sat, 18 Sep 2010 02:51:31 +0000</pubDate>
		<dc:creator>Thomas Larsen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://yabasic.basicprogramming.org/?p=512</guid>
		<description><![CDATA[Yabasic 3 development is now underway! Pedro will be implementing the abstract syntax tree, Jacob will be working on the symbol management routines (that will handle variables and subroutines), and I&#8217;ll be focusing on the parser, lexer, and the backbone &#8230;<p class="read-more"><a href="http://yabasic.basicprogramming.org/yabasic-3-progress-update">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Yabasic 3 development is now underway! Pedro will be implementing the abstract syntax tree, Jacob will be working on the symbol management routines (that will handle variables and subroutines), and I&#8217;ll be focusing on the parser, lexer, and the backbone of the interpreter. Because there are now three people working on Yabasic 3, we plan to use a Git version control <a href="http://github.com/tlarsen/Yabasic/">repository</a> hosted on <a href="http://github.com">GitHub</a>&#8212;there&#8217;s already some code up there, but it&#8217;s not functional yet.</p>
<p>Personally, I hope to get a substantial amount of work done on Yabasic in the next week or so; we&#8217;ll see whether that works out or not. <img src='http://yabasic.basicprogramming.org/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  Thanks to all you Yabasic users out there for your patience!</p>
<p>Here&#8217;s to Yabasic 3&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://yabasic.basicprogramming.org/yabasic-3-progress-update/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Variables in Yabasic 3</title>
		<link>http://yabasic.basicprogramming.org/variables-in-yabasic-3-2</link>
		<comments>http://yabasic.basicprogramming.org/variables-in-yabasic-3-2#comments</comments>
		<pubDate>Sun, 05 Sep 2010 10:06:24 +0000</pubDate>
		<dc:creator>Thomas Larsen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://yabasic.basicprogramming.org/?p=469</guid>
		<description><![CDATA[Variables can identify two types of values: real numbers (no suffix appended to variable name) and character strings (&#8220;$&#8221; suffix appended). Variables in action: pi = 3.1416 name$ = "Bob" Scalars Scalars are variables that can hold only one value: &#8230;<p class="read-more"><a href="http://yabasic.basicprogramming.org/variables-in-yabasic-3-2">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Variables can identify two types of values: real numbers (no suffix appended to variable name) and character strings (&#8220;$&#8221; suffix appended).</p>
<p>Variables in action:</p>
<pre>pi = 3.1416
name$ = "Bob"</pre>
<h2>Scalars</h2>
<p>Scalars are variables that can hold only one value:</p>
<pre>town$ = "Tinyville"
population = 100</pre>
<p>Scalars need not be declared prior to use, but note that an error will be reported if a program tries to access a scalar variable that has not previously been assigned a value.</p>
<h2>Arrays</h2>
<p>Arrays are variables that can hold more than one value:</p>
<pre>town$ (1) = "Randomville"
population (1) = 30000
town$ (2) = "Somewhereton"
population (2) = 20000</pre>
<p>Arrays can have any number of dimensions.</p>
<p>The position of an element in an array is specified using indices. Given an index <em>n</em>, an upper bound <em>b</em>, and assuming that <em>n</em> and <em>b</em> are both members of the set of integers, 0 must be less than or equal to <em>n</em>, and <em>n</em> in turn must be less than or equal to <em>b</em>.</p>
<p>Arrays need not be declared prior to use, but note that an error will be reported if a program tries to access an array element that has not previously been assigned a value.</p>
<h2>Structures and user-defined types</h2>
<p>User-defined types (UDTs) are &#8220;templates&#8221; for structures. They can be built out of scalars, arrays, and structures.</p>
<p>Structures can be explicitly created:</p>
<pre>struct some_structure
   some_component
end struct</pre>
<p>or made from UDTs:</p>
<pre>type some_type
   some_component
end type

struct some_structure_1 as some_type
struct some_structure_2 as some_type</pre>
<p>For example:</p>
<pre>type crate
   name$
   apples
   oranges
   pears
end type

struct crate (2) as crate

crate (1).name$ = "CR001"
crate (1).apples = 30
crate (1).oranges = 50
crate (1).pears = 40

crate (2).name$ = "CR002"
crate (2).apples = 40
crate (2).oranges = 25
crate (2).pears = 50</pre>
<h2>Subroutine arguments/parameters</h2>
<p>Arguments passed to subroutines are stored in variable parameters:</p>
<pre>v (1) = 3
values (1, 2, v ())

sub values (a, b, c ())
   print a, b, c (1)   // output will be "1 2 3"
   return
end sub</pre>
<p>Each argument must correspond to a variable parameter, and vice-versa; an error will be reported if too many or too few arguments are passed to a subroutine.</p>
<p>By default, scalar arguments are passed &#8220;by value&#8221;, and arrays are passed &#8220;by reference&#8221;; however, the keywords <em>byval</em> and <em>byref</em> can be used to override this:</p>
<pre>a = 2
print a   // output will be "2"

assign (a, 7)
print a   // output will be "7"

sub assign (byref scalar, byval value)
   scalar = 7
   return
end sub</pre>
<p>Structures can be passed as subroutine arguments:</p>
<pre>crate.apples = 30
crate.oranges = 50
crate.pears = 40
count_fruit (crate)

sub count_fruit (crate)
   print crate.apples, "apples"     // output will be "30 apples"
   print crate.oranges, "oranges"   // output will be "50 oranges"
   print crate.pears, "pears"       // output will be "40 pears"
   return
end sub</pre>
<h2>Global, local, and static variables</h2>
<p>(Note: For the purpose of clarification, and in light of some flaws in my own thinking, I have edited this section multiple times.)</p>
<p>By default, variables are &#8220;global&#8221;; that is, they can be accessed both from within the main program body and from inside subroutines.</p>
<p>Variables inside subroutines can be declared &#8220;local&#8221;. A local variable can only be accessed from inside its own subroutine; also, a local variable, if one exists, will be always used within a subroutine in preference to a global variable of the same name. For example:</p>
<pre>a = 1
print example_1 (), a   // will print "2 2"

a = 1
print example_2 (), a   // will print "2 1"

sub example_1 ()
   a = 2   // changes the value of the global "a"

   return a
end sub

sub example_2 ()
   local a

   a = 2   // changes the value of the local "a", not that of the global "a"

   return a
end sub
</pre>
<p>By default, a local variable will lose its value when its subroutine returns. A &#8220;static&#8221; variable is a special form of local variable that keeps its value when the subroutine that it is in returns. For example:</p>
<pre>increment_values ()   // output will be "1 2"
increment_values ()   // output will be "2 3"

sub increment_values ()
   static a = 1, b = 2, c () = { 1 }

   print a, b

   return
end sub
</pre>
<p>Static variables must be assigned an initial value.</p>
]]></content:encoded>
			<wfw:commentRss>http://yabasic.basicprogramming.org/variables-in-yabasic-3-2/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Variables in Yabasic 3 (some preliminary ideas, version two)</title>
		<link>http://yabasic.basicprogramming.org/variables-in-yabasic-3-some-preliminary-ideas-version-two</link>
		<comments>http://yabasic.basicprogramming.org/variables-in-yabasic-3-some-preliminary-ideas-version-two#comments</comments>
		<pubDate>Thu, 19 Aug 2010 11:18:16 +0000</pubDate>
		<dc:creator>Thomas Larsen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://yabasic.basicprogramming.org/?p=459</guid>
		<description><![CDATA[Working on the basis that it is prudent to release early and often, here&#8217;s version two. A few notes&#8230; In light of some of the feedback I&#8217;ve received, I think it would be confusing if variables were allowed to &#8220;change &#8230;<p class="read-more"><a href="http://yabasic.basicprogramming.org/variables-in-yabasic-3-some-preliminary-ideas-version-two">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Working on the basis that it is prudent to release early and often, <a href="http://yabasic.basicprogramming.org/wordpress/wp-content/uploads/2010/08/variables-in-yabasic-31.pdf">here&#8217;s version two</a>.</p>
<p>A few notes&#8230;</p>
<ul>
<li>In light of some of the feedback I&#8217;ve received, I think it would be confusing if variables were allowed to &#8220;change types&#8221; mid-program, so I&#8217;ve updated the presentation accordingly.</li>
<li>If we kept the &#8220;$&#8221; suffix for character string variable names, associative arrays might be a little ugly (assuming we use them to implement records): would <code>oranges.name$</code> become <code>oranges$ ("name")</code> or <code>oranges ("name$")</code>? This isn&#8217;t too big an issue, though.</li>
<li>I would suggest that both &#8220;plain&#8221; and &#8220;associative&#8221; arrays work in the same way from a user&#8217;s point of view, but it might be a good idea to detect the difference and represent them differently internally, thus maximising speed?</li>
</ul>
<p>Thanks to everyone who&#8217;s provided feedback so far. Please forgive me if I do not respond to it immediately; I&#8217;m very busy, and keeping track of so many posts is hard enough as it is! <img src='http://yabasic.basicprogramming.org/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  But keep the comments and suggestions coming, folks!</p>
]]></content:encoded>
			<wfw:commentRss>http://yabasic.basicprogramming.org/variables-in-yabasic-3-some-preliminary-ideas-version-two/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Variables in Yabasic 3 (some preliminary ideas)</title>
		<link>http://yabasic.basicprogramming.org/variables-in-yabasic-3-some-preliminary-ideas</link>
		<comments>http://yabasic.basicprogramming.org/variables-in-yabasic-3-some-preliminary-ideas#comments</comments>
		<pubDate>Wed, 18 Aug 2010 12:01:40 +0000</pubDate>
		<dc:creator>Thomas Larsen</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://yabasic.basicprogramming.org/?p=454</guid>
		<description><![CDATA[Hello all! I&#8217;ve been working on some preliminary ideas for variables in Yabasic 3. Here&#8217;s a presentation that outlines what I have in mind. Comments, questions, and suggestions are welcome! Thanks, Tom.]]></description>
			<content:encoded><![CDATA[<p>Hello all!</p>
<p>I&#8217;ve been working on some preliminary ideas for variables in Yabasic 3. Here&#8217;s a <a href="http://yabasic.basicprogramming.org/wordpress/wp-content/uploads/2010/08/variables-in-yabasic-3.pdf">presentation</a> that outlines what I have in mind.</p>
<p>Comments, questions, and suggestions are welcome!</p>
<p>Thanks, Tom.</p>
]]></content:encoded>
			<wfw:commentRss>http://yabasic.basicprogramming.org/variables-in-yabasic-3-some-preliminary-ideas/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Welcome, Jacob!</title>
		<link>http://yabasic.basicprogramming.org/welcome-jacob</link>
		<comments>http://yabasic.basicprogramming.org/welcome-jacob#comments</comments>
		<pubDate>Tue, 10 Aug 2010 11:32:14 +0000</pubDate>
		<dc:creator>Thomas Larsen</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://yabasic.basicprogramming.org/?p=345</guid>
		<description><![CDATA[I&#8217;m delighted to announce that Jacob Nielsen will be joining us in our endeavour to re-write Yabasic in C++. At this stage, he will probably focus primarily on implementing the symbol-handling class, which will manage variables (including scalars and arrays) &#8230;<p class="read-more"><a href="http://yabasic.basicprogramming.org/welcome-jacob">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m delighted to announce that Jacob Nielsen will be joining us in our endeavour to re-write Yabasic in C++. At this stage, he will probably focus primarily on implementing the symbol-handling class, which will manage variables (including scalars and arrays) and subroutines (to a degree). The symbol-handling class will be one of Yabasic&#8217;s core components, and I&#8217;m very pleased that we have someone competent (unlike myself) to work on it.</p>
<p>So, Jacob, welcome! It&#8217;s great to have you on board.</p>
]]></content:encoded>
			<wfw:commentRss>http://yabasic.basicprogramming.org/welcome-jacob/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

