Jump to content
IGNORED

Functions crash my program


MrTrust

Recommended Posts

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?

Link to comment
Share on other sites

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?

  • Thanks 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...