Uncertain about returned value [SOLVED]

Started by Chrille, Sun 17/01/2010 16:45:48

Previous topic - Next topic

Chrille

I'm trying to make a function that looks for a value in an array, but I'm uncertain of how returning values work.

Here's what the function looks like, roughly:

Code: ags
function searchArray (int whichvalue) {
  int search = 0;

  while (search > -1) {
    search ++;
    if (array[search]==whichvalue) {
      ? return true; ?
    }
    if (search == 100) { // 100=array size
      ? return false; ?
    }
  }
}


Here's how I'd like to use it:
Code: ags
if (searchArray(1)==true) Display ("bla bla bla");


But as I said, I'm uncertain of how a certain value is returned. I'd just like to return a true/false value depending on if the wanted value is found in the array or not.
GASPOP software
http://www.gaspop.com

Dualnames

Code: ags
function searchArray (int whichvalue) {
  int search = 0;
bool doit;

  while (search > -1) {
    search ++;
    if (array[search]==whichvalue) {
    doit=true;
      return doit;
    }
    if (search == 100) { // 100=array size
    doit=false;      
   return doit;
    }
  }
}


It would be better to return the int so you can check with values 1 or 0. But there it is with  boolean.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Chrille

Yeah I might as well use an int. Thanks for the example!
GASPOP software
http://www.gaspop.com

Khris

In this case returning the wrong type should work, but in general, if the function is supposed to return a boolean value, it has to look like this:

Code: ags
boolean ArrayContains (int whichvalue) {
  int search = 1;
  while (search <= 100) {
    if (array[search] == whichvalue) return true;
    search++;
  }
  return false;
}


function is merely a substitute for int; in theory, every function should use the return type instead of "function" or "void" if it isn't supposed to return anything.

monkey0506

To clarify that the type for boolean in AGS is bool so it would be:

Code: ags
bool ArrayContains(int whichvalue) {


Also, in AGS the following types all have implicit conversions to int:

bool
char
short
(any enumerated type)

So returning any of those types of values from a function would work. However if you want to return any pointer (such as a Character*, InventoryItem*, etc.), a String, or a float then you must specify the exact type you are working with as there is no implicit conversion possible.

SMF spam blocked by CleanTalk