SA-MP Forums Archive
Functions with an unknown amount of arguments - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Functions with an unknown amount of arguments (/showthread.php?tid=528806)



Functions with an unknown amount of arguments - sammp - 29.07.2014

Okay, waaay back in January I made a function that has an unknown amount of args. I just found it, never really got a solution so I'm troubleshooting :3

pawn Код:
stock AddNumber(...)
    {
        new result = 0;
       
        for(new i = 0; i <= numargs(); ++i)
        {
            result += getarg(i);
        }
        return result;
    }
No compiler errors, Though I'm sure it didn't work.


Re: Functions with an unknown amount of arguments - Sharpadox - 29.07.2014

And whats your problem / question? If your problem is, that your code doenst work, then try this (worked fine):

pawn Код:
stock AddNumber(...)
{
    new result;
    for(new i=0; i<numargs(); i++) result += getarg(i);
    return result;
}



Re: Functions with an unknown amount of arguments - sammp - 29.07.2014

That's the exact same so it should work. I'll try it.


Re: Functions with an unknown amount of arguments - Sharpadox - 30.07.2014

No, its not the same.
Your code doenst work, but my code works :P

And here i tested it:
pawn Код:
#include a_samp
main() {
    printf("%i",AddNumber_Sharpadox(1,2,3,4));  // returned 10
    printf("%i",AddNumber_sammp(1,2,3,4));      // returned 47
}

stock AddNumber_Sharpadox(...)
{
    new result;
    for(new i=0; i<numargs(); i++) result += getarg(i);
    return result;
}

stock AddNumber_sammp(...)
{
    new result = 0;

    for(new i = 0; i <= numargs(); ++i)
    {
        result += getarg(i);
    }
    return result;
}



Re: Functions with an unknown amount of arguments - sammp - 30.07.2014

The methods are the same except you have not nested them..

Yeah, you didn't help me in the slightest as you didn't point out what was wrong! All I had to do was make it < rather than <= and change new result = 0; to new result;

So yeah


Re: Functions with an unknown amount of arguments - Sharpadox - 30.07.2014

And you have to write "i++" and not "++i" :P