Q's of: static, variable, and return functions
#1

Something I can't understand is what is the function of return. I know "return 1" returns 1 as I saw it on wiki, but I don't understand whats the use of it, and also whats the difference between return 1 and return 0, and how can I use it.
Also, I don't understand the difference between a variable and static. They both look the same to me. Please explain, thanks.
Reply
#2

return has different meanings in different functions but the basic use looks like this:

pawn Код:
Function()
{
    return 5;
}

if(Function() == 5)
{
    // true
}
else
{
    // false
}
It returns a value to the place it was called. In that example Function() returns 5.

Variable can be dynamic or static. Difference is that dynamic variable gets deleted when it's not used anymore (out of scope), but static variable is always there (like global variables).

It's all explained in wiki:

return: https://sampwiki.blast.hk/wiki/Control_Structures#return
static/non-static: https://sampwiki.blast.hk/wiki/Scripting_Basics#Scope
Reply
#3

Can you please explain me some things that I couldn't understand by reading in Wiki, just clear it out.

Please tell me how will 'local static' print this, I don't understand, it says on the wiki that it doesn't forget it's old value, but I still can't understand please explain to me. It should rather print 111 because, below j is set to 1 in the loop:
Quote:
Originally Posted by wiki.sa-mp.com
static local

A static local can be used in the same place as a local but doesn't forget it's old value, for example:

MyFunc()
{
static
var1 = 4;
printf("%d", var1);
{
// var1 still exists as this is a lower level
static
var2 = 8;
printf("%d %d", var1, var2);
}
// var2 no longer exists as this is a higher level
}
// var1 no longer exists

That code will behave exactly the same as the new example, however this:

for (new i = 0; i < 3; i++)
{
static
j = 1;
printf("%d", j);
j++;
}

Will print:

1
2
3

Because j is static so remembers its old value.


I also don't understand what does wiki mean in 'global static'. I don't understand what they mean by "they can only be used in the file they are declared in":
Quote:
Originally Posted by wiki.sa-mp.com
global static

Global static variables are like normal globals but can only be used in the file in which they are declared:

File1:

static
gsMyVar = 4;

MyFunc()
{
printf("%d", gsMyVar);
}

#include "File2"

File2:

MyFunc2()
{
// This is wrong as gsMyVar doesn't exist here
printf("%d", gsMyVar);
}

static can also be applied to functions in the same way.

Can someone also explain to me what does return 0 and return 1 have in difference. Please explain me this.
Reply
#4

Global static variables:
Assume you have
pawn Код:
static MyStaticVar = 4; // in the file MyInclude1.inc
// and the variable
new MyGlobalVar = 5;
Now you include MyInclude in your gamemode.
The difference is now that you can use MyGlobalVar in your gamemode but you can't use MyStaticVar because it can only be used in the file where it's declared.

---

Local static variables are like global variables BUT the can only be used in the function where the get declared.
Local static variables do NOT get deleted/reset after the function has been performed.

Example:
pawn Код:
MyFunc1 ()
{
    static var1 = 0;
    var1++;
    printf ("var1 = %d", var1); // Will print 1, 2, 3, 4 => Will increase by 1 everytime the function gets called.
}

MyFunc2 ()
{
    new var2 = 0;
    var2++;
    printf ("var2 = %d", var2); // Will ALWAYS print 1
}
Reply
#5

Any functions can return any value, whether that be a number, float or string. The difference between returning 0 and 1 simply means the function will represent 0 instead of 1. You can also return functions, which in the end, will return the value that that function returns (if it doesn't return anything, the compiler will throw you an error). Let me give you a quick example:
pawn Код:
printf( "%i", MyFunction( random( 5 ) ) );
new x = MyFunction( 4 );
printf( "%i", x );


MyFunction( value )
{
   return value;
}
A ver basic example. MyFunction will return the value that is passed to it. So with the very first line of code demonstrated there, it could return anything from 0 to 3, sense we give it a random value of those numbers to choose from. With the next line, we declare a variable to set a value of 4 (since we passed 4 to the function, it'll return 4), thus giving the variable that value. So when we print the second message, it'll show 4.

For static variables, they remain in memory for the most part, except global like you mentioned, only used in the file they are declared in. The difference between a global static variable and static variable is the same as that as a global variable and a local variable - it's declared outide of any functions. When you declare a global variable, that means it cannot be used outside of the file it was declared in. For example, if you created a global static function inside a library (include) and included it into a gamemode, you couldn't access that variable from the gamemode's source. Local static variables will just remain in memory, thus they don't have to be created over and over again when the function is called. This is why they're suggested to be used in functions that are called a lot (such as a very small timer or OnPlayerUpdate).

You can find more information on the pawn_lang.pdf.

//Late
Reply
#6

pawn Код:
static
    j = 1;
This will set j to 1 when the script starts, it's ignored in the loop.
It's like a global variable that has initial value (usually 0), but the difference is that you can use global variable anywhere, but static local variable has a scope.
Reply
#7

Thanks alot Grim, MadeMan and Double - O - Seven.
And Grim, I've seen people use this:
pawn Код:
OnPlayerRequestSpawn(playerid)
{
    if(IsPlayerNPC(playerid)) return 0;
}
I think I know this much that it will exempt the NPC from the code below return 0;, am I right?
Reply
#8

Quote:
Originally Posted by ||123||
Посмотреть сообщение
Thanks alot Grim, MadeMan and Double - O - Seven.
And Grim, I've seen people use this:
pawn Код:
OnPlayerRequestSpawn(playerid)
{
    if(IsPlayerNPC(playerid)) return 0;
}
I think I know this much that it will exempt the NPC from the code below return 0;, am I right?
Yeah, you are right. https://sampwiki.blast.hk/wiki/OnPlayerRequestSpawn
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)