Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SUB's
#1
So, I'm stuck again.
I tried making some subs, but I still have some questions about it. And yes, I've read tfm. I want to refer from the sub to the main programm (with a GOTO), and that doesn't work. It's something like this:
Code:
declare sub sub1 (action$)
sub1
coords:   'this could have been anywhere
--------
sub sub1
input "choose an action "; action$
if action$="q" then end                      'etc
if action$="l" then goto coords
Is there a better way to refer? When I assign a value in the sub, it isn't remembered in the main program.
Reply
#2
You don't use GOTO to call a SUB.

First of all, you define it this way:

Code:
Syntax
  SUB globalname[parameterlist][STATIC]
    [statements]
  [EXIT SUB]
    [statements]
  END SUB

You can't use GOTO to a label outside the sub, so what you want to do is impossible.

Imagine that you have a SUB defined this way that just prints a text at the x, y coordinates provided:

Code:
SUB printText(x%, y%, text$)
   LOCATE y%, x%
   PRINT text$
END SUB

The way to call a SUB is specified in the good ol' QB help:

Quote:Subprograms are called by a CALL statement or by using the subprogram name
followed by the argument list. See the entry for the CALL statement.

That means that our SUB can be called either with this:

Code:
CALL printText(10, 10, "Hello!")

or with this syntax:

Code:
printText 10, 10, "Hello!"

Finally, you have to remember that...

Quote:Any subprogram variables or arrays are considered local to that sub-
program, unless they are explicitly declared as shared variables in
a SHARED statement.

and, finally:

Quote:Note: You cannot use GOSUB, GOTO, or RETURN to enter or exit a subprogram.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#3
Yes, you are correct. You cannot refer to a label outside of a module. Depending on what you are trying to do with you program there are as many ways of going about it as there are programmers.

Unless you specify a variable to be shared across SUBs using a COMMON SHARED or (RE)DIM SHARED statements, to pass data back and forth from a sub, you need to use arguments.

Code:
INPUT "Write some gibberish please: ", text$
CLS
CENTRE 12, text$
END

SUB CENTRE (row%, text$)
   LOCATE row, 41 - LEN(text$) / 2
   PRINT text$;
END SUB
Code:
'a rather pointless sub but it shows whats happining
a% = 10: b% = 13
PRINT a%,b%
change a%, b%
PRINT a%, b%
END

SUB change (a%, b%)
   SWAP a%, b%
END SUB

<edit=added>Oh... Too slow Smile</edit>
url=http://www.spreadfirefox.com/?q=affiliates&id=60131&t=79][Image: safer.gif][/url]
END OF LINE.
Reply
#4
OK. This manual is different from mine. (Mine says I need to open a file ?remline? to show the way SUB is working, which I don't have.) From now on, i'll call a sub with call, I'm convinced. So it should be
declare sub sub1(blah$,x%,y%,whatever$)
.....
call sub sub1(blah$,x%,y%,whatever$) ' ?i can enter a value here?

And the same thing ( (blah$,x%,y%,whatever$) ) included in the sub. Right, but I still have the same question. I want a SUB (for instance) to move around. In the sub, I want to make a movement trough some sort of coordination, a(n) x and y position. Then, I thought, I could make this (f.e. x=3,y=2) into a coordination point in the main program, and then move there. This is far from serious, I know, but anyway, i'm just trying.
Reply
#5
Quote:call sub sub1(blah$,x%,y%,whatever$) ' ?i can enter a value here?

Close.

Code:
CALL sub1(blah$, x%, y%, whatever$)

You don't need the word SUB in there when calling it Smile
earn.
Reply
#6
declare a sub.

It's automatically declared after you type it in or after you make it from the menu:

type sub blabla in and it makes you a sub, then put in your arguments. After you save, you should see at the top of your main the sub and its parameters.
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#7
Quote:declare sub sub1 (action$)
sub1
coords:   'this could have been anywhere
--------
sub sub1
input "choose an action "; action$
if action$="q" then end                      'etc
if action$="l" then goto coords

Do you mean something like this?

Code:
declare sub sub1 (question$,x%,y%)
call sub1 ("Choose an action",x%,y%)
'it could be: sub1 "Choose an action",x%,y%
print x%,y%
end

sub sub1(question$,x%,y%)
print question$;:input action$
select case  action$
case "q" : end
case "1" :x%=7:y%=4:exit sub
case else:print "Wrong input"
end select
end sub

You use the sub parameters to pass values to the sub and return them back. EXIT SUB leaves inmediately the sub, if you dont use it, END SUB will also leave. Both of them return the program flow to the instruction after where the sub was called.
Antoni
Reply
#8
Yes, this did actually help. At least it returns a value now. Thanks. I'm trying now (doomed to fale) to put a variabele in a goto....That of course doesn't work, so I have to think of another way to do this. But, thanks anyway.
Reply
#9
Quote:OK. This manual is different from mine. (Mine says I need to open a file ?remline? to show the way SUB is working, which I don't have.)
That is because you are using the QBASIC 1.1 help manual, the 4.5 one is better Smile
url=http://www.spreadfirefox.com/?q=affiliates&id=60131&t=79][Image: safer.gif][/url]
END OF LINE.
Reply
#10
Don't speak about GOTO's too loud, a lot of people here is alergic to it.
A go to simply jumps to a label or line number in the same sub or main part of the program. It does'nt take parameters. As it jumps inside the same piece of code you have access to the same variables you were using before the goto.
Antoni
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)