MrTrust Posted June 10, 2021 Share Posted June 10, 2021 Title says it. I'm trying to get a simple function to work: function Rnd temp1 = (rand&63) + (rand&15) + (rand&3) + 7 return temp1 Compiles fine. Immediately crashes Stella. I take the return statement out, and everything's fine again. It won't compile with an end statement, so what the heck? Doesn't matter what I do, either. I have a return statement in my function, Stella just crashes. Very annoying. Anyone got any ideas? Quote Link to comment https://forums.atariage.com/topic/321627-functions-crash-my-program/ Share on other sites More sharing options...
+Karl G Posted June 10, 2021 Share Posted June 10, 2021 This should probably be in the batari Basic subforum. Your example works for me in this tiny sample program: dim sc=score+2 scorecolor = $0F sc=Rnd() Main drawscreen goto Main function Rnd temp1 = (rand&63) + (rand&15) + (rand&3) + 7 return temp1 I'm wondering if the issue is how you called your function. Did you remember the empty parenthesis (since you aren't passing anything to your function)? If you are still having issues, then maybe attach a sample program that demonstrates the problem? 1 Quote Link to comment https://forums.atariage.com/topic/321627-functions-crash-my-program/#findComment-4840942 Share on other sites More sharing options...
MrTrust Posted June 10, 2021 Author Share Posted June 10, 2021 Of course, the one place I didn't try moving it was at the very end of the program. I thought it would make sense to put it at the beginning where I'm initializing variables, then I thought maybe it needs to be in the main loop because the temp variable needs to be zeroed out first, then I pasted it all over the place trying to see if that would give me a different result except at the end of the program. Dumb on my part, I guess, but also, why does that matter? The code should only execute if the function is called, right? Anyway, thanks a million for the tip; you just saved me about 200 badly-needed bytes. Mods, is there any way we can move this to the appropriate subforum? My mistake. Quote Link to comment https://forums.atariage.com/topic/321627-functions-crash-my-program/#findComment-4840965 Share on other sites More sharing options...
+Karl G Posted June 10, 2021 Share Posted June 10, 2021 5 minutes ago, MrTrust said: Of course, the one place I didn't try moving it was at the very end of the program. I thought it would make sense to put it at the beginning where I'm initializing variables, then I thought maybe it needs to be in the main loop because the temp variable needs to be zeroed out first, then I pasted it all over the place trying to see if that would give me a different result except at the end of the program. If it is at the beginning of the program, it will try to execute the function. The crash happens when it reaches the "return" because the code executes without being called, so there's no address on the stack for the return. Functions, subroutines, inline assembly modules should all go somewhere where they will not be executed unless called, such as after the main loop. 1 Quote Link to comment https://forums.atariage.com/topic/321627-functions-crash-my-program/#findComment-4840972 Share on other sites More sharing options...
MrTrust Posted June 11, 2021 Author Share Posted June 11, 2021 6 hours ago, Karl G said: If it is at the beginning of the program, it will try to execute the function. Wow, what a dumbass. I know better than that. Duh, a function or subroutine statement is no different than an old fashioned line number. Somewhere in my head, I got this subconcious idea that all these things were like if-then statements; they'll just be skipped over unless a call is made. Of course, even if-thens aren't really skipped, right? Something's got to into the accumulator for them to work, right? I get so cart-before-the-horse when programming (or trying to) that I do stupid stuff like this all the time. Is there a word for this, where the programmer implicitly assumes the computer knows what they're talking about? If there isn't, there should be. Quote Link to comment https://forums.atariage.com/topic/321627-functions-crash-my-program/#findComment-4841161 Share on other sites More sharing options...
+Karl G Posted June 11, 2021 Share Posted June 11, 2021 bB and high-level languages are abstractions, so it's possible it could have been designed the way you expected. I think it would probably be good to update the documentation to suggest placing the function definition somewhere where it won't be executed accidentally. Also, the documentation incorrectly says user functions need to be ended with an "end" statement. Quote Link to comment https://forums.atariage.com/topic/321627-functions-crash-my-program/#findComment-4841382 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.