Can someone explain this? (getarg)
#1

Код:
stock SubtractNumber(...)
	{
		new result = 0;
		for(new i = 0; i < numargs(); ++i)
		{
			result = result - getarg(i);

			printf("Arg %d = %d", i,getarg(i));
		}
		return result;
	}
If I input 15, 5 and 4, it outputs -24. So it basically just adds them and inverts it. Why does it do this? (This function is just for testing purposes)
Reply
#2

(0 - (15 - 5) - 4) = -24.

"result" starts from zero and subtracts from there, which is the reason why you're getting -24.

To start at 15 you could do:

pawn Код:
stock SubtractNumber(start, ...)
{
    new result = start;
    for(new i = 1, l = numargs(); i < l; ++i)
    {
        result = result - getarg(i);

        printf("Arg %d = %d", i,getarg(i));
    }
    return result;
}
pawn Код:
printf("%i", SubtractNumber(15, 5, 4));
Outputs:

pawn Код:
6
Reply
#3

I'm in idiot haha, didn't think of that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)