SUB's - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: QBasic (http://qbasicnews.com/newforum/forum-4.html) +--- Forum: QB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-11.html) +--- Thread: SUB's (/thread-1150.html) |
SUB's - banAnnA - 06-10-2003 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$) SUB's - na_th_an - 06-10-2003 You don't use GOTO to call a SUB. First of all, you define it this way: Code: Syntax 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$) 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 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- and, finally: Quote:Note: You cannot use GOSUB, GOTO, or RETURN to enter or exit a subprogram. SUB's - Phydaux - 06-10-2003 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$ Code: 'a rather pointless sub but it shows whats happining <edit=added>Oh... Too slow </edit> SUB's - banAnnA - 06-10-2003 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. SUB's - seph - 06-10-2003 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 you don't need to - Agamemnus - 06-10-2003 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. SUB's - Antoni Gual - 06-11-2003 Quote:declare sub sub1 (action$) Do you mean something like this? Code: declare sub sub1 (question$,x%,y%) 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. SUB's - banAnnA - 06-12-2003 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. SUB's - Phydaux - 06-12-2003 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 SUB's - Antoni Gual - 06-12-2003 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. |