So... I don't know BASIC super well, but I do know Lua, and in Lua you can call defined functions. For example;

function abc() <-- Define function abc
print("ABC") <-- Basically Lua's version of DISP
end <-- End function abc

abc() <-- Call function abc


This code would print "ABC" to the output. Now, the best I've managed to do to achieve this effect with basic is use Goto's and Lbl's. These are extremely inefficient and confounding when you try to do anything of any real size, and the program I'm trying to make branches so much that I rather dislike the idea of a million If, Then, and Else statements. In Lua, you could do something like the following;

function ABCandDEF() <-- define function ABCandDEF
abc() <-- Call function abc (below)
(Point A)
def() <-- Call function def (below)
(Point C)
end <-- Close function ABCandDEF
(Point E)

function abc() <-- defines function abc, like in the first example
(Point B)
print("ABC")
end

function def() <-- defines function def, just like abc
(Point D)
print("ABC")
end

ABCandDEF() <-- calls function ABCandDEF


The function ABCandDEF would first call abc (Point A), causing the program to jump there (Point B) and execute all the way through function abc, then jump back to the next line of ABCandDEF (Point C), in this case being the line, "def()", which would call def, jumping to that function (Point D), executing through that, then back to the next line of ABCandDEF (Point E), which in this case is an 'end', stopping the program.

So... The ultimate question... Is there a way to mimic this in BASIC (w/o Lbl's and Goto's)? A way to write and call functions, then return to the place where they were called?
In BASIC that is impossible. One solution would be indeed a bunch of Lbl and Goto (although that may cause a memory leak). Another tricky one, when not storing variables or displaying stuff, use expr (string). The last one you can try is something with the recursive u, but my acknowledge of that is low. I hope this answer helped. If not, feel free to ask!
What is the Return command for then?
You can call programs as if they were subroutines. Return ends the current program, returning to a calling program if one exists. Stop on the other hand terminates all active execution.

Dumb recursive approach to displaying Ans lines of ".", assuming it's prgmDOTS:
Code:
:If Ans<1
:Return
:Disp ".
:Ans-1
:prgmDOTS
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement