How to use return 1/0 ?
#1

Hi there!

I can't understand when i have to use return 1 or return 0 in a function.

For example, which is good?


Code:
if(listitem == 0)
			{
				if(PlayerData[playerid][pLocateCar] == 1)
				{
					format(szMessage, sizeof(szMessage), "Your car (Model: %s) has been respawned!", VehicleNames[PlayerData[playerid][pCarModel1] - 400]);
					SendClientMessage(playerid, COLOR_WHITE, szMessage);
				}
			}
or

Code:
if(listitem == 0)
			{
				if(PlayerData[playerid][pLocateCar] == 1)
				{
					format(szMessage, sizeof(szMessage), "Your car (Model: %s) has been respawned!", VehicleNames[PlayerData[playerid][pCarModel1] - 400]);
					SendClientMessage(playerid, COLOR_WHITE, szMessage);
				}
                                return 1;
			}
When and why?
Reply
#2

Hello!

It's both the same only it return one times 1 and one times 0.
But it is both the same.

This is better as you don't use return.
PHP Code:
if(listitem == 0)
{
    if(
PlayerData[playerid][pLocateCar] == 1)
    {
        
format(szMessage,sizeof szMessage,"Your car (Model: %s) has been respawned!",VehicleNames[PlayerData[playerid][pCarModel1]-400]);
        
SendClientMessage(playerid,COLOR_WHITE,szMessage);
        return 
1;
    }

Reply
#3

if(listitem == 0){
if(PlayerData[playerid][pLocateCar] == 1)
{
format(szMessage, sizeof(szMessage), "Your car (Model: %s) has been respawned!", VehicleNames[PlayerData[playerid][pCarModel1] - 400]);
SendClientMessage(playerid, COLOR_WHITE, szMessage);
return 1;
}
}
Reply
#4

Quote:
Originally Posted by Mencent
View Post
Hello!

It's both the same only it return one times 1 and one times 0.
But it is both the same.

This is better as you don't use return.
PHP Code:
if(listitem == 0)
{
    if(
PlayerData[playerid][pLocateCar] == 1)
    {
        
format(szMessage,sizeof szMessage,"Your car (Model: %s) has been respawned!",VehicleNames[PlayerData[playerid][pCarModel1]-400]);
        
SendClientMessage(playerid,COLOR_WHITE,szMessage);
        return 
1;
    }

1. Is absolutely necessary to use a return value there? (Why?)
2. if i will forget to use a return value, is it a problem ? It can be an issue for bad script working?
Reply
#5

Code:
if(listitem == 0){
if(PlayerData[playerid][pLocateCar] == 1)
{
format(szMessage, sizeof(szMessage), "Your car (Model: %s) has been respawned!", VehicleNames[PlayerData[playerid][pCarModel1] - 400]);
SendClientMessage(playerid, COLOR_WHITE, szMessage);
return 1;
}
}
Reply
#6

1. Is absolutely necessary to use a return value there? (Why?)
2. if i will forget to use a return value, is it a problem ? It can be an issue for bad script working?
Reply
#7

Returning a value is usually optional (though it will usually give a warning), though it can be useful and or needed in some situations. When simply responding to a callback typically returning 1 would be practical unless the callback does different things(such as a OnTakeDamage function not giving damage if 0 is returned) when other things are returned.

Though for functions returns can sometimes be useful & or needed. For example:
pawn Code:
public OnPlayerStateChange(playerid, newstate, oldstate) {
     if(newstate == PLAYER_STATE_DRIVER && IsACopVehicle(GetPlayerVehicleID(playerid)) {
           if(!IsACop(playerid)) return RemovePlayerFromVehicle(playerid);
     }

     return 1;
}
In this case, IsACop and IsACopVehicle return different values. This allows you to check if(if it returns 1) the player is a cop and if it returns 0 the player isn't a cop.

Also 1/0 aren't the only values you can return. You can return strings, and other integer values as well. NOTE: Due to an issue with the compiler directly returning a string will crash the compiler(e.g: return "yes". This works in old compiler versions and also if you're using Zeex's compiler patches(I believe).
Reply
#8

1.) It is better, otherwise the codes continue also if you don't want it.

2.) No, it is not a problem but the codes will continue also if you don't want it.
Reply
#9

Quote:
Originally Posted by Abagail
View Post
Returning a value is usually optional (though it will usually give a warning), though it can be useful and or needed in some situations. When simply responding to a callback typically returning 1 would be practical unless the callback does different things(such as a OnTakeDamage function not giving damage if 0 is returned) when other things are returned.

Though for functions returns can sometimes be useful & or needed. For example:
pawn Code:
public OnPlayerStateChange(playerid, newstate, oldstate) {
     if(newstate == PLAYER_STATE_DRIVER && IsACopVehicle(GetPlayerVehicleID(playerid)) {
           if(!IsACop(playerid)) return RemovePlayerFromVehicle(playerid);
     }

     return 1;
}
In this case, IsACop and IsACopVehicle return different values. This allows you to check if(if it returns 1) the player is a cop and if it returns 0 the player isn't a cop.

Also 1/0 aren't the only values you can return. You can return strings, and other integer values as well. NOTE: Due to an issue with the compiler directly returning a string will crash the compiler(e.g: return "yes". This works in old compiler versions and also if you're using Zeex's compiler patches(I believe).
Thank you, but in this case is absolutely necessary?

Code:
if(listitem == 0) 
{ 
    if(PlayerData[playerid][pLocateCar] == 1) 
    { 
        format(szMessage,sizeof szMessage,"Your car (Model: %s) has been respawned!",VehicleNames[PlayerData[playerid][pCarModel1]-400]); 
        SendClientMessage(playerid,COLOR_WHITE,szMessage); 
        return 1; 
    } 
}
Reply
#10

The return statement is used to return a value back from a function, not necessarily 1 or 0. For instance

PHP Code:
function adda){
     return 
b;
}

new 
result add5); // result is now 11 
If you're meaning sa-mps public functions/callbacks like OnPlayerSpawn etc then it really depends on the callback. Sometimes returning will cancel the effects of the the callback. For instance https://sampwiki.blast.hk/wiki/OnPlayerWeaponShot states that returning 0 will cause the bullet to do no damage, but returning 1 will allow it to damage as usual. Other callbacks such as OnCommandText returning 0 will tell it that you did not handle the command and will continue to on into other filterscripts until it receives return 1, if not the command was not handle and it will notify the player of Command does not exist or whatever the message is. Other callbacks the return value doesn't matter. Check wiki if you're unsure
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)