|This tutorial will show you how to allow a function to change a variable that is not part of the function.|
As you learned in past tutorials, when you pass the value of a variable to a function you use an argument such as function_name($argument). This creates a "local copy" of the original variable which will be manipulated by the function. Any change to the local copy of the variable within the function does not affect the value of the original (outside) variable. But, there are times when you want to use the function to change the value of the original (outside) variable; not just the value of the copy. How would you do this?
You can reference, or point to, the original variable when you pass an argument to the function. By doing this, you can manipulate the value of the original (outside) variable from within the function. Let's take a look at a sample code.
As you can see, this is your basic function code with one exception. The difference is the ampersand (&) in front of the argument ($number) and this points to (reference) the outside variable $start_no. At the beginning of the function, the value of the $start_no variable is 2.
This line of code multiplies the $number variable by 3. At this point, the original value of the $start_no variable (2) has been changed to 6.
This sets the initial value of the $start_no variable to 2. This is a value of the outside variable at the time of the function call.
This is the function call.