PHP Kod:
Functions
Functions, as stated before, are isolated blocks of code that perform an action. They can be invoked, or called, with parameters that give specific options.
There are two types of ways functions are called:
direct call - You specifically call a function in your code.
callback - The application calls a function in your code, as if it were an event trigger.
There are six types of functions:
native: A direct, internal function provided by the application.
public: A callback function that is visible to the application and other scripts.
normal: A normal function that only you can call.
static: The scope of this function is restricted to the current file, can be used in combination with stock.
stock: A normal function provided by an include file. If unused, it won't be compiled.
forward: This function is a global event provided by the application. If you implement it, it will be a callback.
All code in Pawn must exist in functions. This is in contrast to languages like PHP, Perl, and Python which let you write global code. That is because Pawn is a callback-based language: it responds to actions from a parent application, and functions must be written to handle those actions. Although our examples often contain free-floating code, this is purely for demonstration purposes. Free-floating code in our examples implies the code is part of some function.
Declaration
PHP Kod:
Declaration
Unlike variables, functions do not need to be declared before you use them. Functions have two pieces, the signature and the body. The signature contains the name of your function and the parameters it will accept. The body is the contents of its code.
Example of a function:
int AddTwoNumbers(int first, int second)
{
int sum = first + second;
return sum;
}
This is a simple function. The prototype is this line:
int AddTwoNumbers(int first, int second)
Broken down, it means:
int - Return value type (integer).
AddTwoNumbers - Name of the function.
int first - First parameter, an integer.
int second - Second parameter, an integer.
The body is a block of code. It creates a new variable, called sum, and assigns it the value of the two parameters added together (more on expressions later). The important thing to notice is the return statement, which tells the function to end and return a value to the caller of the function. All functions return something on completion, unless they return a special type called void.
A function can accept any type of input, and it can return any non-array type.
You can, of course, pass variables to functions:
int numbers[3] = {1, 2, 0};
numbers[2] = AddTwoNumbers(numbers[0], numbers[1]);
Note that cells are passed by value. That is, their value cannot be changed by the function. For example:
int a = 5;
ChangeValue(a);
void ChangeValue(int b)
{
b = 5;
}
This code would not change the value of a. That is because a copy of the value in a is passed instead of a itself.
More examples of functions will be provided throughout the article.
PHP Kod:
Scope
Scope refers to the visibility of code. That is, code at one level may not be "visible" to code at another level. For example:
int A, B, C;
void Function1()
{
int B;
Function2();
}
void Function2()
{
int C;
}
In this example, A, B, and C exist at global scope. They can be seen by any function. However, the B in Function1 is not the same variable as the B at the global level. Instead, it is at local scope, and is thus a local variable.
Similarly, Function1 and Function2 know nothing about each other's variables.
Not only is the variable private to Function1, but it is re-created each time the function is invoked. Imagine this:
void Function1()
{
int B;
Function1();
}
In the above example, Function1 calls itself. Of course, this is infinite recursion (a bad thing), but the idea is that each time the function runs, there is a new copy of B. When the function ends, B is destroyed, and the value is lost.
This property can be simplified by saying that a variable's scope is equal to the nesting level it is in. That is, a variable at global scope is visible globally to all functions. A variable at local scope is visible to all code blocks "beneath" its nesting level. For example:
void Function1()
{
int A;
if (A)
{
A = 5;
}
}
The above code is valid since A's scope extends throughout the function. The following code, however, is not valid:
void Function1()
{
int A;
if (A)
{
int B = 5;
}
B = 5;
}
Notice that B is declared in a new code block. That means B is only accessible to that code block (and all sub-blocks nested within). As soon as the code block terminates, B is no longer valid.
Ayrıca static const char ne demek her biri ayrı ayrı ve tam anlamıyla char pawn dilinde string karşılığı ama diğerlerini anlamadım static ve const( bu sadece tek bir değer alabilir ve değişemez biliyorum sadece)