SA-MP Forums Archive
[Tutorial] Pawn Language Tutorial[Functions] - 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)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Pawn Language Tutorial[Functions] (/showthread.php?tid=321465)



Pawn Language Tutorial[Functions] - [HK]Ryder[AN] - 27.02.2012

Functions
A function is a group of statements that is executed when it is called from some point of the program.
A function declaration specifies the name of the function and, between parentheses, its formal parameters. A function may also return a value. A function declaration must appear on a global level (i.e. outside any other functions) and is globally accessible.
Example:
pawn Код:
myfunc(a, b)   // "a" and "b" are the parameters
The return statement sets the function result.
return expression
Terminates the current function and moves program control to the statement following the calling statement. The value of the expression is returned as the function result. The expression may be an array variable or a literal array. The expression is optional, but it must start on the same line as the return statement if it is present. If absent, the value of the function is zero.
pawn Код:
myfunc(a, b)
{
    return a + b
}
 
main()
{
    printf "%d", myfunc(4, 2)  // 6
}
Arguments of a function are (implicitly declared) local variables for that function. The function call determines the values of the arguments.
A function may contain multiple return statements —one usually does this to quickly exit a function on a parameter error or when it turns out that the function has nothing to do. If a function returns an array, all return statements must specify an array with the same size and the same dimensions.

Function Arguments (call-by-value versus call-by-reference)
Take a look at this example:
pawn Код:
main()
{
    new x = 2, y = 4
    myfunc(x, y)
    printf "%d %d", x, y  // 2 4
}
 
myfunc(a, b)
{
    a += 2
    b += 4
    printf "%d %d\n", a, b // 4 8
}
In the myfunc function the variables are passed “by value”, meaning that only a copy of their values are passed and NOT the variables themselves. So any modifications done to the parameters inside a function will NOT affect the variables passed as arguments in the call to that function.
When a variable is passed “by reference” we are not passing a copy of its value, but we are somehow passing the variable itself to the function and any modification that we do to the local variables will have an effect in their counterpart variables passed as arguments in the call to the function.
Arguments that occupy a single cell can be passed by value or by reference. The default is “pass by value”. To create a function argument that is “passed by” reference, prefix the argument name with the character &.
pawn Код:
main()
{
    new x = 2, y = 4
    myfunc(x, y)
    printf "%d %d", x, y    // 4 8   // the variables have been modified
}
 
myfunc(&a, &b)    // passed by reference
{
    a += 2
    b += 4
    printf "%d %d\n", a, b    // 4 8
}
Calling Functions
When inserting a function name with its parameters in a statement or expression, the function will get executed in that statement/expression. The statement that refers to the function is the “caller” and the function itself, at that point, is the “callee”: the one being called. The standard syntax for calling a function is to write the function’s name, followed by a list with all explicitly passed parameters between parentheses.
If no parameters are passed, or if the function does not have any, the pair of parentheses behind the function name are still present.
A function may optionally return a value. Even if a function returns a value, the caller may ignore it.
The parentheses around all function arguments are optional if the caller does not use the return value. Example:
pawn Код:
main()
{
    new x = 2, y = 4
    myfunc x, y   // here I ignore the result returned by myfunc
                          // the parentheses are optional
    printf "%d %d", x, y   // 4 8
}
 
myfunc(&a, &b)
{
    a += 2
    b += 3
    return a + b
}
The syntax without parentheses around the parameter list is called the “procedure call” syntax. You can use it only if:

the caller does not assign the function’s result to a variable and does not use it in an expression, or as the “test expression” of an if statement for example; Default values of Function Arguments
A function argument may have a default value. The default value for a function argument must be a constant. To specify a default value, append the equal sign (“=”) and the value to the argument name. When the function call specifies an argument placeholder instead of a valid argument, the default value applies. The argument placeholder is the underscore character (“_”). The argument placeholder is only valid for function arguments that have a default value. The rightmost argument placeholders may simply be stripped from the function argument list. For example:
pawn Код:
main()
{
    new a = 4
    increment(a)  // equivalent to
    increment(a, _)  // equivalent to
    increment(a, 1)
}
 
increment (&val, inc=1)   // inc has a default value of 1
{
    val += inc
}
Recursivity
Recursivity is the property that functions have to be called by themselves. It is useful for many tasks, like sorting or calculate the factorial of numbers. Example:
pawn Код:
main()
{
    printf "%d", factorial(5)  // 120
}
 
factorial(n)
{
    if(n == 0)  // this is the "stop condition"
        return 1  // without it we would have an infinite recursion
    else
        return n * factorial(n-1)   // the function calls itself
}
Native Functions
A pawn program can call application-specific functions through a “native function”. The native function must be declared in the pawn program by means of a function prototype. The function name must be preceded by the keyword native.
pawn Код:
native getparam(a[], b[], size)
native multiply_matrix(a[], b[], size)
native openfile(const name[])
The names “getparam”, “multiply_matrix” and “openfile” are the internal names of the native functions; these are the names by which the functions are known in the pawn program. Optionally, you may also set an external name for the native function, which is the name of the function as the “host application” knows it. To do so, affix an equal sign to the function prototype followed by the external name. For example:
pawn Код:
native getparam(a[], b[], size) = host_getparam

Hope you enjoyed my tutorial.I will be adding more parts to it such as Syntax,Data & Declarations,Control Structures etc later when i have time.
Thanks.
this has been taken from the web.


Respuesta: Pawn Language Tutorial[Functions] - [Nikk] - 27.02.2012

Exlent tutorial, good for advanced scripters.


Re: Pawn Language Tutorial[Functions] - [HK]Ryder[AN] - 27.02.2012

Thanks!!


Re: Pawn Language Tutorial[Functions] - aksharma97 - 27.02.2012

Very good tutorial.Great for good scripters!!


Re: Pawn Language Tutorial[Functions] - [HK]Ryder[AN] - 27.02.2012

Thanks!


Re: Pawn Language Tutorial[Functions] - T0pAz - 27.02.2012

http://thedarkjoker94.cer33.com/?cat=52


Re: Pawn Language Tutorial[Functions] - [HK]Ryder[AN] - 27.02.2012

Right again
Taken from him


Re: Pawn Language Tutorial[Functions] - aco_SRBIJA - 27.02.2012

Recursivity .....I hate it the mostt! Most hardest function...You must have big IQ to know how to use thems perfectly..


Re: Pawn Language Tutorial[Functions] - FarSe. - 27.02.2012

Nothing about '...' (numargs,setarg,getarg) and funcidx, they also are a part of PAWN Functions.


Re: Pawn Language Tutorial[Functions] - Ballu Miaa - 27.02.2012

Taken from somewhere! But yeah it should be here too!