whats the point of return
#1

as the topic says whats the point of putting return 1 and return 0 and sometimes not even putting return 0 or 1, why dont we just put

pawn Код:
public shit
{

};

&

stock something
{

};
Reply
#2

pawn Код:
myFunction(a, b)
{
    return a + b;
}

myOtherFunction()
{
    new val1 = 5;
    new val2 = 7;

    new output = myFunction(val1, val2); // output now contains 12
}
Very simplistic example, performing a calculation. In other circumstances people tend to return 0 to indicate something failed. e.g.:

pawn Код:
doSomethingImportant()
{
    if(someCondition == false)
        return 0;

    // do the stuff
    return 1;
}

myfunction()
{
    if(doSomethingImportant())
    {
        print("yeey!");
    }
    else
    {
        print("failed");
    }
}
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
pawn Код:
myFunction(a, b)
{
    return a + b;
}

myOtherFunction()
{
    new val1 = 5;
    new val2 = 7;

    new output = myFunction(val1, val2); // output now contains 12
}
Very simplistic example, performing a calculation. In other circumstances people tend to return 0 to indicate something failed. e.g.:

pawn Код:
doSomethingImportant()
{
    if(someCondition == false)
        return 0;

    // do the stuff
    return 1;
}

myfunction()
{
    if(doSomethingImportant())
    {
        print("yeey!");
    }
    else
    {
        print("failed");
    }
}
Is it compulsory to put return, i heard in some cases it may cause lag or leakage or whatever..?
Reply
#4

In commands, if you don't return 0 you'll get "ERROR: Unknown Command" or something
Reply
#5

It really depends on what your doing some callbacks have an effect when returning 1 or 0 such as OnPlayerUpdate() other times you might want to return a value indicating if a function failed or not and sometimes you don't even need to return anything at all however I like to put return 1; to indicate the end of a function.
Reply
#6

For the callbacks, just read the given documentation (https://sampwiki.blast.hk/wiki/Scripting_Callbacks)

For the functions, it's just like in C. If you need to return something useful in a function, then return it.
Else, just don't return anything anywhere (using "return;" returns also nothing).

At last but not least : once you used "return" somewhere in your code, if this "return" is accessed run-time, then the program won't go forward.

For example :

pawn Код:
myFunc(...)
{
      if(numargs() == 0) // If no arguments passed
            return; // Exit the function
      if(getarg(0) == 13) // If the first argument's value is 13 (we can do it because we are sure the user have entered at least an argument with the condition above.
            printf("The first argument is 13 !"); // print a message
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)