Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem w/ subs
#1
why does this not work?

Code:
option explicit

declare sub message_selector(a)
declare sub message_one(message)
declare sub message_two(message2)

dim a as integer
dim message as string
dim message2 as string

message = "what the f***"
message2 = "stfu!"

randomize timer

goto message_selector(a)

sub message_selector(a)
do
a = rnd * 3
if a = 1 then goto message_one(message) end if
if a = 2 then goto message_two(message2) end if
loop
end sub

sub message_one(message)
    print message
    goto message_selector(a)
end sub

sub message_two(message2)
    print message2
    goto message_selector(a)
end sub
url=http://www.random-seed.net][Image: asylumsig.png][/url]
Reply
#2
remove all gotos.

if you HAD to you could use CALL, thats proper syntax for ... calling :p a sub. but call is just empty baggage, like LET.

also, message_selector doesnt need to be passed ( a ), its never passed back. just dim a within the sub instead
Reply
#3
here dude, your code had errors... to say the least... just look at this and ask if you have q's

Code:
Option Explicit

Declare Sub message_selector
Declare Sub _message ( m As String )

Dim Shared message As String
Dim Shared message2 As String

message = "what the f***"
message2 = "stfu!"


Randomize Timer

message_selector

Sub message_selector

  Dim a As Integer
  
  Do
    a = Int( Rnd * 2 )
    
    If a = 0 Then _message ( message )
    If a = 1 Then _message ( message2 )
    
  Loop Until Inkey$ = Chr$( 27 )

End Sub

Sub _message ( m As String )


    Print m


End Sub
Reply
#4
yeah, whats with the dim shared?
url=http://www.random-seed.net][Image: asylumsig.png][/url]
Reply
#5
ok, here we go, i revized the entire thing...

Code:
option explicit

randomize timer

declare sub message()
declare sub message_two()
declare sub message_selector()

message_selector

sub message_selector()
    dim sub_pointer as integer
    do
    sub_pointer = rnd * 3
    if sub_pointer = 1 then call message() end if
    if sub_pointer = 2 then call message_two() end if
    sleep 500
    loop
end sub

sub message()
    print "WTF"
    call message_selector()
end sub

sub message_two()
    print "stfu"
    call message_selector()
end sub

is that pretty good? the point of the exersize is to make a program that selects random subs
url=http://www.random-seed.net][Image: asylumsig.png][/url]
Reply
#6
the subs cant acess module level variables. so, your flow is like this


main -> message_selector -> message

all the stuff is in main, but once u get to msg selector, you lose all those main variables (except SHARED or passed vars). thats called "scope"

to recap, shared lets all subs acess those messages without having to pass them
Reply
#7
aaah! wow, thas really kool... so if i want to use the variable "A" in my mainloop, and in sub1 and sub2, i could dim it as shared, then there you go? wow... pretty freakin sweet
url=http://www.random-seed.net][Image: asylumsig.png][/url]
Reply
#8
3 things.

1, don't call back from the subs, thats what you call "infinite recursion", youll run out of stack space after a while, and crash. just let the subs return naturally.

2, rnd * 3 will give you a number from 0 to 3, so 0 and 3 wont call any sub. perhaps this is the intended effect.

3, check this out ;)


Code:
Option Explicit

Randomize Timer

Declare Sub message()
Declare Sub message_two()
Declare Sub message_selector()

message_selector

Sub message_selector()


  Dim sub_pointer (2) As Sub
  Dim selector As Integer

  sub_pointer (1) = @message()
  sub_pointer (2) = @message_two()
  
  Do
    selector = Rnd * 2  
    If selector <> 0 Then sub_pointer ( selector ) ()

    Sleep 500
    
  Loop
  
End Sub

Sub message()

  Print "WTF"

End Sub

Sub message_two()

  Print "stfu"

End Sub

now it really is a "sub pointer" ;)
Reply
#9
i kinda see whats happening there, but i dont think i'm at the level of using that in my code... i'm not a jedi yet
url=http://www.random-seed.net][Image: asylumsig.png][/url]
Reply
#10
for future reference ;)

its extremely handy for handling states, instead of like select case case 1 (code) case 2 (code) ... case 78 (code) lols. u can just set pointers like i did, and then call the sub number with the code in it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)