Q's of: static, variable, and return functions
#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


Messages In This Thread
Q's of: static, variable, and return functions - by ||123|| - 22.07.2011, 10:25
Re: Q's of: static, variable, and return functions - by MadeMan - 22.07.2011, 10:45
Re: Q's of: static, variable, and return functions - by ||123|| - 22.07.2011, 11:35
Re: Q's of: static, variable, and return functions - by Double-O-Seven - 22.07.2011, 11:42
Re: Q's of: static, variable, and return functions - by Grim_ - 22.07.2011, 11:46
Re: Q's of: static, variable, and return functions - by MadeMan - 22.07.2011, 11:53
Re: Q's of: static, variable, and return functions - by ||123|| - 22.07.2011, 12:01
Re: Q's of: static, variable, and return functions - by Roko_foko - 22.07.2011, 12:16

Forum Jump:


Users browsing this thread: 2 Guest(s)