Author Topic: Help with function  (Read 419 times)

Legacy_Nebril2

  • Full Member
  • ***
  • Posts: 104
  • Karma: +0/-0
Help with function
« on: November 08, 2012, 01:14:49 pm »


               Hi all!!



Im having a problem with a function.
I want to make a script that calls a function that inside the function returns a random string value.

Example: function

string/void? treasure(){string objeo;  int azarss =Random(3)+ 1;   if (azarss ==(1))   return objeo("a");   if (azarss ==(2))   return objeo ( "b");   if (azarss ==(3))
   return objeo ( "c");

//This gave me an error "ERROR: PARSING RETURN STATEMENT"

So when i include the function on a script, i want that every time the function is called it returns a random value inside the function treasure and that value to be saved on a string variable inside the script. 

Any better way to do this?

Thanks!'<img'>
               
               

               
            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
Help with function
« Reply #1 on: November 08, 2012, 01:32:53 pm »


               

Nebril2 wrote...

Hi all!!

Im having a problem with a function.
I want to make a script that calls a function that inside the function returns a random string value.

Example: function

string/void? treasure(){string objeo;  int azarss =Random(3)+ 1;   if (azarss ==(1))   return objeo("a");   if (azarss ==(2))   return objeo ( "b");   if (azarss ==(3))
   return objeo ( "c");

//This gave me an error "ERROR: PARSING RETURN STATEMENT"

So when i include the function on a script, i want that every time the function is called it returns a random value inside the function treasure and that value to be saved on a string variable inside the script. 

Any better way to do this?

Thanks!'<img'>



string treasure()
{
 string objeo;
switch (Random (3) + 1)
    {
     case 1: objeo = "a"; break;
     case 2: objeo = "b"; break;
     case 3: objeo = "c"; break;
    }
 return objeo;
}


Alternately, you can just return the strings directly, as:

   if  (azarss == 1)   return "a";
               
               

               


                     Modifié par Failed.Bard, 08 novembre 2012 - 01:34 .
                     
                  


            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Help with function
« Reply #2 on: November 08, 2012, 01:33:28 pm »


               (1) the label before the function name defines the type of value returned by the function. In your case this should be string
(2) objeo as you have described it is a string variable, not a function which returns a string.

if your function named treasure is to return a string (I assume the string is a resref for a treasure blueprint), then you should do as follows:

string treasure()
{
  string objeo; // this is a variable. it will hold the string that is returned by the function treasure
  int azarss =Random(3)+ 1;
  if (azarss==1)
    objeo = "a";
  else if (azarss==2)
    objeo = "b";
  else if (azarss==3)
    objeo = "c";

  return objeo;
}

[edit: ooops. failed bard got to this first. ]
               
               

               


                     Modifié par henesua, 08 novembre 2012 - 01:34 .
                     
                  


            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
Help with function
« Reply #3 on: November 08, 2012, 01:36:08 pm »


               I didn't explain the label part, and likely should have.  Also, Henesua, yours uses the if/else if chain, and mine switch/case, so likely both are handy to have as examples regardless.
               
               

               
            

Legacy_Nebril2

  • Full Member
  • ***
  • Posts: 104
  • Karma: +0/-0
Help with function
« Reply #4 on: November 08, 2012, 02:01:51 pm »


               Well that was a fast solution '<img'> worked!

Thank you very much guys