LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

GOSUB not working!

Root / Programming Questions / [.]

Jacklack3Created:
I'm trying to make my own commands using GOSUB, but the problem is i get a error when doing this.
GOSUB @TEST
PRINT"HELLO2!

@TEST
PRINT"HELLO1!
RETURN
The error is
RETURN WITH NO GOSUB
How can i fix this?

WHILE 1
 GOSUB @test
 PRINT "BLOH
 WAIT 1
 CLS
WEND

@TEST
print "BLAH"
RETURN

Your version wasn't working because it would Gosub right but when it returned it goes 1 line down in this case the line PRINT "HELLO1". then it would proceed down to the RETURN when a gosub hadn't been called yet. Understand?

The problem is that the code at the label @TEST gets executed even after you GOSUB to it. If a RETURN is given and it shouldn't be there, it will error. So the error is completely normal.
GOSUB @TEST
PRINT"HELLO2!
END
@TEST
PRINT"HELLO1!
RETURN
That should give you zero errors, but you should learn about user defined functions. I'll give you a sample.
TEST
PRINT "WORLD"

DEF TEST
 PRINT "HELLO"
END
The output would be:
HELLO
WORLD
However you can pass parameters, but I'm not trying to go over complicated with stuff. Check the help for DEF if you need more info.

People have already answered you, but I'd like to try and make it clearer just in case. Here is your code, with line numbers added:
1   GOSUB @TEST
2   PRINT"HELLO2!
3
4   @TEST
5   PRINT"HELLO1!
6   RETURN
What happens is: The program starts on line 1 - which tells it to gosub to @TEST (line 4). Next line to run is 5, so it prints "HELLO1". Then on line 6, the program returns back to line 1. Then comes line 2, so it prints "HELLO2!". Then comes line 3. This line is blank, so nothing happens. Then comes line 4. This line has a label on it (@TEST), and nothing else, so the program keeps going. Then comes line 5, so it prints "HELLO1!" (again. Then comes line 6, and this time, we've reached it outside of the GOSUB. This is why you get an error. Final output should be:
HELLO1! (from within GOSUB)
HELLO2! (line 2)
HELLO1! (reached by continuing after line 2)
<error message>

Also I'd suggest using DEF to make your own commands.
TEST
PRINT"HELLO2!
END

DEF TEST
PRINT"HELLO1!
END
Will work the same way.

All you have to do is add an END on line 3.

All you have to do is add an END on line 3.
That WILL work in this case, but I don't think it will with what they are trying to do.

The problem is that the code at the label @TEST gets executed even after you GOSUB to it. If a RETURN is given and it shouldn't be there, it will error. So the error is completely normal.
GOSUB @TEST
PRINT"HELLO2!
END
@TEST
PRINT"HELLO1!
RETURN
That should give you zero errors, but you should learn about user defined functions. I'll give you a sample.
TEST
PRINT "WORLD"

DEF TEST
 PRINT "HELLO"
END
The output would be:
HELLO
WORLD
However you can pass parameters, but I'm not trying to go over complicated with stuff. Check the help for DEF if you need more info.
Yea i found out DEF. im using that now. Thanks for the help anyways guys!