basicprogramming.org


Welcome, Guest. Please login or register.
Did you miss your activation email?
Forum time; Jul 31. 2010, 03:43
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 1228 times)
Derek
Administrator
Hero Member
*****
Offline Offline

Posts: 1001



WWW
« Reply #15 on: Jun 24. 2008, 11:04 » Reply with quote

Hi,

Code:
print "The value passed to \"test()\" will be halved and returned to the main routine."
print "test(123) = ", test(123)
AnotherVar = test(123) * 4
print "AnotherVar = ", AnotherVar
print "test(234) = ", test(234)
AnotherVar = test(234) * 4
print "AnotherVar = ", AnotherVar
print "test(1024 * 64) = ", test(1024 * 64)
AnotherVar = test(1024 * 64) * 4
print "AnotherVar = ", AnotherVar
end
//
sub test(number)
local result
result = number / 2
return result
end sub
Read the code and look at the results it gives. test() just divides a number by two so doesn't have much use in the real world but it's a nice example.

Aprint() or pt() will not print to the graphic window because it uses "print". To write text to the graphic window you'll have to use the "text" command to do that.
Code:
sub MyText(text$)
text text$, 100, 100 // :-/ I can't remember the format of the text command without reading the manual :)
end sub

If you're still stuck please give us an example of one of the concepts you're struggling with and one of us will explain Smiley

Please feel free to ask us anything about this, there is no such thing as silly question Smiley

Derek.
Report to moderator   Logged

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

This is good, I only have one question though,

Making a print sub,

Code:
sub Aprint(test$)
print test$
end sub

When using a parameter with numbers, e.g pause or wait command.

Code:
wait 50

How would I go about doing this,

Code:
number$ or number?

something like that?

Mainchip.
Report to moderator   Logged
Mainchip
Guest
« Reply #17 on: Jun 25. 2008, 08:32 » Reply with quote

Never mind I worked this out myself. Cheers.

Mainchip.
Report to moderator   Logged
E.K.Virtanen
Blah
Hero Member
*****
Offline Offline

Posts: 2441



WWW
« Reply #18 on: Jun 25. 2008, 08:44 » Reply with quote

Hi Mainchip.
How did you worked this one?
Report to moderator   Logged

Mainchip
Guest
« Reply #19 on: Jun 25. 2008, 10:33 » Reply with quote

I did:

Code:
sub wt(number)
wait(number)
end sub

Which worked fine for me  Smiley

EDITED: I am now going to try make a long calculation sub  Smiley

Mainchip.
« Last Edit: Jun 25. 2008, 10:35 by Mainchip » Report to moderator   Logged
Mainchip
Guest
« Reply #20 on: Jun 25. 2008, 13:49 » Reply with quote

Hey guys,

I tried to change input to ip by doing this:

Code:
sub ip(text$)
input text$
end sub

There is no errors it just won't display the text on input what have I done wrong?

Mainchip
Report to moderator   Logged
E.K.Virtanen
Blah
Hero Member
*****
Offline Offline

Posts: 2441



WWW
« Reply #21 on: Jun 25. 2008, 13:57 » Reply with quote

Since text$ is for printing, then you need to do it this way.

Code:
sub ip$(text$)
     local a$
     print text$;
     input a$
end sub

In your case, yaBasic thinks text$ is for user input.
Report to moderator   Logged

Mainchip
Guest
« Reply #22 on: Jun 25. 2008, 14:21 » Reply with quote

Oh right okay, ahh this is were I need that all important local command  Wink

EDITED: There is actually a problem with this as-well:

Code:
ip("What is your name?") name$

Would not work because of the variable.

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

Posts: 1001



WWW
« Reply #23 on: Jun 25. 2008, 15:06 » Reply with quote

Oh right okay, ahh this is were I need that all important local command  Wink

EDITED: There is actually a problem with this as-well:

Code:
ip("What is your name?") name$

Would not work because of the variable.

Mainchip.

Hi,

you are passing a parameter by placing it outside the brackets which isn't allowed. If you want to get the inputted text back then it would looks like this:
Code:
inputted_text$ = ip$("What is your name? ")

You're expecting something back from the sub so it has to be passed back using the "return" command. I'll let you figure it out from here Smiley

Derek.
Report to moderator   Logged

wget http://*
Mainchip
Guest
« Reply #24 on: Jun 25. 2008, 15:14 » Reply with quote

LOL I can't figure this out I don't have the knowledge  Sad also the yabasic manual doesn't say anything about stuff like this. It needs updating  Angry. I promise when I eventually master yabasic in the next few years i am going to rewrite that bloody thing lol.

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

Posts: 1001



WWW
« Reply #25 on: Jun 25. 2008, 15:59 » Reply with quote

Hi,

don't give up! You're asking the right questions and all the info you need is here. Forget about local variables for the time being, they may be confusing the issue at the moment.

Take a break; read a book, watch telly, listen to some music, do something that will take your mind of this and when you come back it will be clearer.

I don't want to give too much help because if you can figure this out for yourself you'll certainly remember it.

For the time being I'll recap the structure of a subroutine:
Code:
sub Example()
sub Example(Parameter1)
sub Example(Parameter1, Parameter2$)
1. The command sub tells Yabasic to expect a subroutine with the name immediately following it and continuing until the open bracket.
2. All parameters must be inside the brackets. There is no limit to the number of params allowed and passing zero params is perfectly fine.
Code:
sub Double(Param1)
Param1 = Param1  * 2
...
Inside the subroutine you can do whatever calculations or printing or inputting or drawing etc to make the sub do what it should.
Code:
sub Double(Param1)
Param1 = Param1  * 2
return Param1
Returning a value from a subroutine is not obligatory but it is a useful aspect of these things that make it one of the more powerful parts of Yabasic.
Code:
sub Double(Param1)
Param1 = Param1  * 2
return Param1
end sub
All end sub does is tell Yabasic that the subroutine definition has finished.

Now, if I wanted to use a subroutine in a script I would do something like this:
Code:
clear screen
Print "I'll think of a number between one and ten and you have three goes at guessing what it is."
target = int (ran (10) + 1)
try = 0
Complete = False
repeat
   try = try + 1
   print "Try number ", try
   input "Have a guess... " guess
   Complete = is_guess_correct(guess, target)
until (try = 3 or Complete = True)
if Complete = False then
   print "Bad luck... The number you wanted was ", target
else
   print "Well done..."
end if

sub is_guess_correct(first_number, second_number)
if first_number = second_number then
   return True
else
   return False
end if
end sub
This is a little guessing game that uses a subroutine to test if the player has guessed the correct number and I'm putting it here because it is a complete script that may help.

While using subroutines is like creating your own commands or functions they have to stick with the same basic structure.

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

wget http://*
Mainchip
Guest
« Reply #26 on: Jun 25. 2008, 16:58 » Reply with quote

Nice example Derek, it has cleared a-lot up for me and shows me the layout of subroutines. In your post before
Quote
You're expecting something back from the sub so it has to be passed back using the "return" command. I'll let you figure it out from here.

You said I need to use the return command, although from what I know you use that with gosub correct? I have no gosub. I kind of see what your trying to explain to me with these lines of your example:

Code:
Complete = False
Complete = is_guess_correct(guess, target)
sub is_guess_correct(first_number, second_number)
return True

Hmm...

I'm going to have a think about all this. Does it really help listening to music lol?
Report to moderator   Logged
Mainchip
Guest
« Reply #27 on: Jun 25. 2008, 17:47 » Reply with quote

Thank god I got it to work although not as I would like it to. Here is what I have:

Code:
ip$("What is your name")


sub ip$(text$)
print text$;
input ip$
end sub

print ip$

This just prints out the input, but it doesn't allow me to set my own variable, I  could use:

Code:
name$ = ip$

so I can print the name with name$ but it will change once I use the subroutine again, I could use local but then it will forget it. How could I use my own variable?

Mainchip.
Report to moderator   Logged
PetrSchreiber
Full Member
***
Offline Offline

Posts: 100


WWW
« Reply #28 on: Jun 26. 2008, 03:48 » Reply with quote

Hi Mainchip,

I am not sure if I understood you well, but here is a code anyway Smiley
Subroutine can return a value, this is done using "return" keyword ( like in C ):
Quote
' -- Main code
result$ = ip$("What is your name")
print "So your name is "+result$+", ... that's nasty"

' -- Subroutines
sub ip$(text$)
' -- Temp variable to store input
  local temp$

' -- Print the prompt
  print text$;

' -- Ask for reply
  input temp$

' -- Return reply from subroutine
  return temp$
end sub

Hope it helps,
Petr
« Last Edit: Jun 26. 2008, 03:51 by PetrSchreiber » Report to moderator   Logged

Mainchip
Guest
« Reply #29 on: Jun 26. 2008, 07:42 » Reply with quote

Yes this does help a lot thanks  Cheesy

Why do I have to write the variable with + either side of it?

Mainchip.
« Last Edit: Jun 26. 2008, 07:46 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!