Functions with an unknown amount of arguments
#1

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.
Reply
#2

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;
}
Reply
#3

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

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;
}
Reply
#5

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
Reply
#6

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


Forum Jump:


Users browsing this thread: 1 Guest(s)