Problem with "return" values in module functions (SOLVED)

Started by Akumayo, Tue 20/12/2005 20:32:55

Previous topic - Next topic

Akumayo

Well... now that I think about it, it's not a probelm per-say, but a question that an answer to will solve a possible problem (confusing I know)

Anyway, what all does the "return" keyword do?Ã,  I have it in my module function several times under different if statements, it should return 1 under some circumstances, and 0 under others.Ã,  It sometimes returns what it shouldn't though, and though it may be a scripting fault on my part, I would like to know exactly what the "return" keyword does, if anything, when called in a function (aside from returning the following value of course)

-Regards, Akumayo
"Power is not a means - it is an end."

DoorKnobHandle

The "return" keyword stops the function and returns the following value. If you only put "return;" then you don't pass a value (maybe in a void-type function) but you make sure that the functions is not processed any further.

Example:

Code: ags

void DrawBullet ( int x, int y )
{
   if ( x < 0 || y < 0 )
   // if either or both x and y are less than zero
   {
      // then display an error and don't continue this function
      ShowError ( "Invalid position at %d,%d in for DrawBullet!", x, y );
      return;
   }
   
   // an "else" statement is now obsolete since the function will
   // only even get to this part if x and y are both greater or equal to zero
   RawSetColor ( 3 );
   RawDrawWhatever ( x, y );

   // now we are done with the function
}

Akumayo

#2
Another thing then, what does a function return if no "return" value is called.Ã,  Ex:

Code: ags

function WhatDoesThisEqual() {
Ã,  //do nothing
}


Code: ags

if (WhatDoesThisEqual() == 0) {
Ã,  //do stuff
}


Will "WhatDoesThisEqual" return 0, or an undefined value of some sort?
"Power is not a means - it is an end."

Gilbert


strazer

Quote from: Pumaman on Sun 01/08/2004 05:18:21
Quote from: Radiant on Fri 23/07/2004 14:45:12a function like ObjectOn() can be used as a rvalue; it is unclear what, if anything, it returns

Originally I wanted the script compiler to be very simple, and as such all functions always returned an int. I wish I hadn't done it that way now, because it can lead to problems if you try and use the return value by accident.
The return value is indeed undefined in these cases, so I really wouldn't recommend trying to do anything with it.

Akumayo

IS it undefined though, or is that just what Radiant thought?Ã,  How can you make any value undefined anyway?Ã,  Is it passed as null perhaps?
"Power is not a means - it is an end."

strazer

It is a quote by CJ, containing a quote by Radiant.

In my experience some random useless number is returned and you shouldn't make any assumptions about what it will be.

Akumayo

I seeeee....  I'm going to find the random useless number... and use it to my advantage... wait... that can't be done... if it's random, can it?  Oh well.  I'll investigate.
"Power is not a means - it is an end."

Gilbert

You're not sure if they're random or not (I'm not sure, but I think user-defined functions will return 0 if you didn't return anything), the problem is, why would you actually need the returned values if they're useless? If you really want random numbers, just use Random().

Akumayo

I tinkered with it a bit, and it does return 0, problem solved.
"Power is not a means - it is an end."

SMF spam blocked by CleanTalk