|
发表于 2006-5-22 16:07:57
|
显示全部楼层
来自 中国–湖北–武汉
回复: stock是定义什么?和public有什么区别?
[color="Blue"] Public functions, function main
A stand-alone program must have the function main. This function is the
starting point of the program. The function main may not have arguments.
A function library need not to have a main function, but it must have it either
a main function, or at least one public function. Function main is the primaryentry point into the compiled program; [color="Red"]the public functions are alternative entry points to the program. The virtual machine can start execution with one of the public functions. A function library may have a main function to perform one-time initialization at startup.
To make a function public, prefix the function name with the keyword public. Functions whose name starts with the "@" symbol are also public. The "@" character, when used, it is a part of the function name.
Arguments of a public function may not have default values.
A public function interfaces the host application to the pawn script. Hence, the arguments passed to the public function originate from the host application, and the host application cannot know what "default values" the script writer plugged for function arguments which is why the pawn parser ags the use of default values for arguments of public functions as an error. The issue of default values in public function arguments only pops up in the case that you wish to call public functions from the script itself.
[color="Blue"] Stock functions
[color="Red"]A "stock" function is a function that the pawn parser must "plug into" the program when it is used, and that it may simply "remove" from the program (without warning) when it is not used. Stock functions allow a compiler or interpreter to optimize the memory footprint and the file size of a (compiled) pawn program: [color="Red"]any stock function that is not referred to, is completely skipped as if it were lacking from the source file.
A typical use of stock functions, hence, is in the creation of a set of "library"
functions. A collection of general purpose functions, all marked as "stock" may
be put in a separate include file, which is then included in any pawn script.
Only the library functions that are actually used get "linked" in.
To declare a stock function, prefix the function name with the keyword stock.Public functions and native functions cannot be declared "stock".
When a stock function calls other functions, it is usually a good practice to
declare those other functions as "stock" too with the exception of native
functions. Similarly, any global variables that are used by a stock function should in most cases also be defined "stock". The removal of unused (stock)
functions can cause a chain reaction in which other functions and global variables
are not longer accessed either. Those functions are then removed as well,
thereby continuing the chain reaction until only the functions that are used,
directly or indirectly, remain. |
|