basicprogramming.org


Welcome, Guest. Please login or register.
Did you miss your activation email?
Forum time; Jul 31. 2010, 03:40
Home Help Search Calendar Login Register
News: Have you got suggestions for BasicProgramming.org? Let's hear them!
Interested in creating your own programming language? Check out the QDepartment group!

+  BASIC programming forum
|-+  Basic Coding
| |-+  General Basic Programming
| | |-+  Q&A
| | | |-+  Yabasic - Subroutine
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2 3 Go Down Reply Print
Author Topic: Yabasic - Subroutine  (Read 1227 times)
Mainchip
Guest
« on: Jun 22. 2008, 10:05 » Reply with quote

Hey everyone, I have been reading the Yabasic documentation (which I think needs updating) and I can't understand the use or how to use subroutines.

How somebody please explain and maybe make me a example.

Thank you,

Mainchip.
Report to moderator   Logged
Derek
Administrator
Hero Member
*****
Offline Offline

Posts: 1001



WWW
« Reply #1 on: Jun 22. 2008, 10:54 » Reply with quote

Hi,

Code:
print multiply(10,4)
print multiply(3.141,4)
print result
end

sub multiply(a,b)
local result
result=a*b
return result
end sub
This trivial example multiplies two numbers together and returns the result which is then printed. Printing var "result" proves that declaring the var "result" as local it will be "forgotten" after the subroutine finishes. The vars "a" and "b" are automatically declared as local.

Subroutines are used to save having to write out the same pieces of code.

Do you want to know anything specific?

Derek.
Report to moderator   Logged

wget http://*
Mainchip
Guest
« Reply #2 on: Jun 22. 2008, 12:05 » Reply with quote

I think that's all I needed to know thanks Derek, So if I had a calculation of some kind and I needed to use it over and over again I could use a Subroutine. Do I have to do the local result?

EDITED: Also you named the sub multiply(a,b) this is kind of like making your own commands right?

Mainchip.
« Last Edit: Jun 22. 2008, 12:12 by Mainchip » Report to moderator   Logged
E.K.Virtanen
Blah
Hero Member
*****
Offline Offline

Posts: 2441



WWW
« Reply #3 on: Jun 22. 2008, 12:52 » Reply with quote

Also you named the sub multiply(a,b) this is kind of like making your own commands right?

Yeah, you could say so.

Code:
sub cls()
clear screen
end sub

sub amid(text$, i)
return asc(mid$(text$, i, 1))
end sub

// common way to clear screen in basic dialects is 'cls'
// now we have it in yaBasic too
cls()

// now we want to know what is 5'th character in ascii
// word is "www.basicprogramming.org"
// so sub amid is like mid$, except it returns ascii val.

print "Text is: www.basicprogramming.org"
print "Fifth character in ascii is: ", amid("www.basicprogramming.org", 5)
end
Report to moderator   Logged

Mainchip
Guest
« Reply #4 on: Jun 22. 2008, 13:52 » Reply with quote

I like that example, with this though is it possible to great your own interpreter?

Also if possible how would I go about changing PRINT command?


Mainchip.
« Last Edit: Jun 22. 2008, 13:53 by Mainchip » Report to moderator   Logged
Derek
Administrator
Hero Member
*****
Offline Offline

Posts: 1001



WWW
« Reply #5 on: Jun 22. 2008, 14:03 » Reply with quote

Do I have to do the local result?

Hi,

you don't have to use "local" but it is very useful. Normally variables can be read from the anywhere in the program but using "local" with a program tells Yabasic to forget about it outside of the routine it is used. By using local you can have as many different variables called "result" as you want. Suppose you have routines "multiply", "add", "minus", etc they can all use the variable "result" without one subroutine changing the value of "result" in another. The other option would be to think up totally unique names for every variable you use.

This is an excellent example:
Quote
I was asked about taking on a contract to maintain a piece of software. Something about the way it was presented made me wary. I asked to look over it first. What a sight! I use it as an example of why not to use global variables. Among other things, there were files with suites of functions on the following order:

adjust_alpha()
{
    alpha = gamma + offset * 3;
}

adjust_beta()
{
    beta = gamma + offset * 3;
}

Dozens of functions that differed only by the global variable they modified. Just picture it: a multi-thousand line program with a graphical interface and a database that never used function parameters.

The original programmer painted himself into a corner with his variable names. Clearly if you need variables "up," "down," "left," and "right," you name them as such. When he found himself needing those direction names in different parts of his program but was stuck because global variable names had to be unique, his solution was to use names like:

up, _up, up_, Up, uP, UP, _Up, _UP
down, _down, down_, Down, dOWN, DOWN, _Down, _DOWN

...and so on. Even the densest of my students comprehended immediately why that was bad. Needless to say, I turned down the job.
from Rinkworks computer stupidities

Derek.
Report to moderator   Logged

wget http://*
E.K.Virtanen
Blah
Hero Member
*****
Offline Offline

Posts: 2441



WWW
« Reply #6 on: Jun 22. 2008, 14:07 » Reply with quote

I like that example, with this though is it possible to great your own interpreter?
Yes, basicly it is possible but not necessarely best way.
Quote
Also if possible how would I go about changing PRINT command?
Since yaBasic does not support function overloading, you need to create 2 functions. For strings and numbers or you need to pass numerical value as text to your print function.

Code:
// Example to create your own print subroutine.
sub APrint(text$)
     // this aprint sub does not move cursor for next line.
     print text$;
end sub

APrint("Text here")
APrint(str$(12345))  // this needs str$ function to work.

Report to moderator   Logged

Mainchip
Guest
« Reply #7 on: Jun 23. 2008, 13:16 » Reply with quote

You say it won't move the cursor for a new line, how can I make a new line then?

Mainchip.
Report to moderator   Logged
LazyEKV
Guest
« Reply #8 on: Jun 23. 2008, 13:39 » Reply with quote

No new line: print text$;
New line (aka default way): print text$

Difference is last char';' Wink
Report to moderator   Logged
Mainchip
Guest
« Reply #9 on: Jun 23. 2008, 13:48 » Reply with quote

For a minute there I thought you was pulling my leg, I couldn't see a difference lol.

Who thinks of these things, what I want to know how do you memorize all this code?


Mainchip.
Report to moderator   Logged
Jacob
Sr. Member
****
Offline Offline

Posts: 414


« Reply #10 on: Jun 23. 2008, 13:56 » Reply with quote

You don't memorize these things, you use them over and over again until they become second nature.
Report to moderator   Logged

"The truth is rarely pure and never simple."  - Oscar Wilde
Mainchip
Guest
« Reply #11 on: Jun 23. 2008, 15:16 » Reply with quote

okay, so learning how things work to start of with is the correct way.
Report to moderator   Logged
Mainchip
Guest
« Reply #12 on: Jun 23. 2008, 15:27 » Reply with quote

Quote
APrint(str$(12345))  // this needs str$ function to work.

What did you mean by this, what is this? Just printing numbers? If so you don't need str$ to print numbers.

EDITED: Also lets say I changed PRINT to:

Code:
sub pt(text$)
print text$
end sub

So print is not pt. Why is their a problem with this:

Code:
name$ = "Mainchip"
pn("Hello", name$, "welcome.")

Seems to be a problem with subroutine.

Mainchip.
« Last Edit: Jun 23. 2008, 15:39 by Mainchip » Report to moderator   Logged
Derek
Administrator
Hero Member
*****
Offline Offline

Posts: 1001



WWW
« Reply #13 on: Jun 23. 2008, 15:56 » Reply with quote

Hi,

Quote
APrint(str$(12345))  // this needs str$ function to work.

The subroutine "Aprint" accepts one parameter which is a string called text$
Code:
sub Aprint(text$)
print text$
end sub
so only strings can be passed to Aprint() which is why numbers have to be converted to strings. "Aprint" is not an exact replica of print as this proves:

Code:
name$ = "Mainchip"
pn("Hello", name$, "welcome.")
(I assume you meant pt(...) ) You are passing three parameters to the subroutine ("Hello", name$ and "welcome.") when it expects and can only cope with one parameter: text$. When you try and pass more parameters to a subroutine than is needed then Yabasic will (thankfully) complain.

Derek.
« Last Edit: Jun 23. 2008, 16:00 by Derek » Report to moderator   Logged

wget http://*
Mainchip
Guest
« Reply #14 on: Jun 23. 2008, 16:56 » Reply with quote

Oh, I see. You gave me text$ as a parameter, what others could I use?

Quote
(I assume you meant pt(...) )

Yes you where correct.  Cheesy

EDITED: Also test$ allows you to change print to e.g Aprint, Does this work with text in the graphic window?

Mainchip.
« Last Edit: Jun 23. 2008, 17:14 by Mainchip » Report to moderator   Logged
Pages: [1] 2 3 Go Up Reply Print 
« previous next »
Jump to:  
Atom RDF RSS 0.91 RSS 2.0


Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!