Functions

Sometimes, a piece of code will be reused several times within the same script. To avoid rewriting the same text many times, it is good practice to encapsulate this part of the program in a function that gets called as many times as needed. It saves time programming, makes the final script shorter and when a bug is found, all parts of the program that use this code will benefit from fixes and optimizations at once. Functions are defined with the word “function” followed by a function name and many times by a list of parameters that allow to customize function results. Here is an example of a function: function AddNumber(a,b) { $returnVal=a+b;  //do whatever needs to be done (in the case add numbers) return $returnVal; //then optionally return a value } then, once a function is defined, it can be called as many times with different parameters, to yield the results needed.