Stock Warning
#1

Hi, i wrote a function for making a calculation but it keeps giving warnings. I tried to fix warning but in that case it gives another warning. Those warnings doesn't affecting something but it's the only warning at my script and this is annoying me.

Code Option #1
Warning: warning 225: unreachable code (pointing "return -1;")
Код:
stock stockname(some params)
{
	//my code here
	for(;;i++)
	{
	   //more code
	    if(...)
		    return i;
	}
	return -1;
}
Code Option #2
Warning: function "stockname" should return a value
Код:
stock stockname(some params)
{
	//my code here
	for(;;i++)
	{
	   //more code
	    if(...)
		    return i;
	}
}
How can i get rid of that warning?
Reply
#2

Both warnings are pretty self-explanatory. Do you mind showing what's inside the for loop?
Reply
#3

Quote:
Originally Posted by zPain
Посмотреть сообщение
Both warnings are pretty self-explanatory. Do you mind showing what's inside the for loop?
Warnings are pretty clear but i still can't get rid of them. Or should i have used a do-while loop

Код:
stock stockname(param)
{
	new i=1, mp=4, pp=2, total=0;
	for(;;i++)
	{
	    total += (i * mp) + pp;
	    if(total >= param)
		    return i;
	}
	//return -1;
}
Reply
#4

First one appears because its a never ending loop so you'll never get to return -1.

The second one is what compiler expects to get but it doesn't know ots an infinite loop.

To avoid, you'll need a limit (some max/min value) for your loop.
Reply
#5

Quote:
Originally Posted by Gammix
Посмотреть сообщение
First one appears because its a never ending loop so you'll never get to return -1.

The second one is what compiler expects to get but it doesn't know ots an infinite loop.

To avoid, you'll need a limit (some max/min value) for your loop.
Alright, while i was editing the loop with unreachable max value, figured out a new way. Its fixed by now.

Код:
stock stockname(param)
{
	new i=1, mp=4, pp=2, total=0, re=-1;
	for(;;i++)
	{
	    total += (i * mp) + pp;
	    if(total >= param)
            {
                  re=i;break;
            }
	}
	return re;
}
Reply
#6

... just use a do while loop, also use constants and remove useless variables
PHP код:
stock stockname(param) {
    const
        
mp 4,
        
pp 2
    
;
    new
        
i
    
;
    do {
        
param -= (++mp) + pp;
    } while(
param);
    return 
i;

Reply
#7

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
... just use a do while loop, also use constants and remove useless variables
PHP код:
stock stockname(param) {
    const
        
mp 4,
        
pp 2
    
;
    new
        
i
    
;
    do {
        
param -= (++mp) + pp;
    } while(
param);
    return 
i;

Changed mine with yours. Thanks.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)