SA-MP Forums Archive
whats the point of return - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: whats the point of return (/showthread.php?tid=544216)



whats the point of return - Bondage - 31.10.2014

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
{

};



Re: whats the point of return - Vince - 31.10.2014

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");
    }
}



Re: whats the point of return - Bondage - 31.10.2014

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..?


Re: whats the point of return - sammp - 31.10.2014

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


Re: whats the point of return - Pottus - 31.10.2014

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.


Re : whats the point of return - S4t3K - 01.11.2014

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
}