|
|
#1 |
|
High-roller
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2007
Posts: 3,472
Reputation: 226
|
Some questions that could be discussed in this thread:
When should we use a function? How should we use a function? What parameters should the function have? Which should be optional? When should a function be stock? When should a function be public? My views: Functions (also called 'subroutines') are useful when you want to execute same code many times, only with some small differences each time. One way to solve this problem would be using copy-paste and then change the parts that need to be changed. Another (better) method is to construct a function. The famous Godfather edits are good examples of scripts that don't use functions very much (or use them inefficiently). Example taken from a Godfather edit /family command (also known as /f and /faction): pawn Code:
Similar pattern continues until 'member' reaches 16. It does work, but is this the best way to do it? As you can see, the lines hardly differ. Only things that change are rank id and rank name. And there are other commands that need to display the same ranks. What if we want to change the name of a rank? We would have to find ALL the places in the script where it occurs and change them. 'format' is used 16 times, 'SendFamilyMessage' 2 times. There must be a way to make it work with only 1 'format' and only 1 'SendFamilyMessage'. This is the point where a function should be made which decides what rank name should be displayed. Function planning: 1) the function of the function (what should the function do?) It should return a string with the rank name of a family 2) the name of the function It should be descriptive enough, so you can get the main idea of what the function does just by looking at the name of it. Let's call ours 'GetFamilyRankName' 3) parameters (what should be the input?) Rank name depends on family id and rank id, so we need to input them as parameters pawn Code:
4) writing the function pawn Code:
After creating the GetFamilyRankName function, our example will turn into this pawn Code:
with only 1 format and 1 SendFamilyMessage used. Now, if you want to change a rank or add a rank, all you have to do is make the needed changes in GetFamilyRankName and that's it! You don't need to even touch the commands that use rank names. *** If you are making a function that does something with a string that is passed as a parameter, you might need to know the size of that string. For example when using strins in it. Compiling something like this pawn Code:
will give Code:
warning 224: indeterminate array size in "sizeof" expression (symbol "maxlength") pawn Code:
Then you can still call the function like this pawn Code:
without needing to worry about the size of the string. *** When should a function be stock? I add 'stock' in front of functions that are independent. By that I mean they can be used in any other script without the need to modify them Examples of stocks pawn Code:
pawn Code:
When should a function be public? Only when it's called ... 1) ... by a timer (SetTimer/SetTimerEx) 2) ... from another script (CallRemoteFunction) 3) ... from the server (callbacks) If the function doesn't fall into one of these categories, it doesn't need to be public. This section of the forums is for discussing and so is this thread. You can add new points, explain your own views/scripting habits, add new questions to be discussed - anything related to (PAWN) functions. |
|
|
|
|
|
#2 |
|
High-roller
![]() ![]() ![]() ![]() ![]() Join Date: Dec 2009
Location: Argentina
Posts: 4,385
Reputation: 160
|
A function is not the same as a subrutine. The main difference is that a sub rutine just execute code in a certain order, while a function do stuff with the parameters passed and may return a value, an other big difference between them is that a function should not change values outside the function itself while a subrutine may change some external values.
|
|
|
|
|
|
#3 |
|
High-roller
![]() ![]() ![]() ![]() ![]() Join Date: Sep 2007
Location: Belgium
Posts: 4,883
Reputation: 1414
|
"It should be noted that no ethically-trained software engineer would ever consent to write a DestroyBaghdad procedure. Basic professional ethics would instead require him to write a DestroyCity procedure, to which Baghdad could be given as a parameter." - Nathaniel Borenstein
Now, for the rank example: personally, I use constant arrays for those. Of course they don't need to be constants, but I do not intend on changing these via the script so they might as well be. Now, whenever I want to retrieve the rank, I simply pass the player's value as the index parameter (if that makes sense) without having to resort to large switch statements; pawn Code:
Of course this is not the entire code and additional checks are in place here to guard against OOB errors. |
|
|
|
|
|
#4 | |
|
High-roller
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2007
Posts: 3,472
Reputation: 226
|
Quote:
|
|
|
|
|
|
|
#5 |
|
High-roller
![]() ![]() ![]() ![]() ![]() Join Date: Aug 2010
Location: INDIA !!! :)) Punjab
Posts: 1,076
Reputation: 107
|
Ty , i hardy able to create differnce b/w a stock function and a simepel function [excluding callbacks]
this helped me clarifying a bit :P |
|
|
|
|
|
#6 |
|
Big Clucker
![]() ![]() Join Date: Oct 2010
Location: France
Posts: 150
Reputation: 11
|
When I scripted my own FS, I noticed that this kind of functions are very useful, so even if I already know that, nice job, it will help more than one scripter
|
|
|
|
|
|
#7 |
|
Huge Clucker
![]() ![]() ![]() Join Date: Feb 2012
Location: ►░▒▓♫♪▓▒░◄
Posts: 362
Reputation: 15
|
Hm, this is going to be useful and helpful for me if I wanted to make the admins show with their ranks, thank you
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| MySQL R6 functions with R7 functions | Tee | Scripting Help | 1 | 25/06/2012 03:08 AM |
| [Include] [INC] LSF - Lorenc's Simple Functions (w/ gang/clan functions) | Lorenc_ | Includes | 11 | 03/05/2010 09:47 PM |
| My Functions.inc | mini-d | Help Archive | 6 | 27/08/2009 06:10 AM |
| [Include] [INC] Two useful functions | Paladin | Includes | 2 | 25/08/2009 01:55 PM |
| What this functions do? | harrold | Help Archive | 2 | 08/05/2009 11:30 PM |