In programming the main goal is to automate or simplify a task as efficiently as possible. It is often said that lazy people make good programmers since the measure of success is achieving the best output for the least input. "Work smart, not hard" encapsulates this particularly well, provided the quality of the end result doesn't suffer. One way to improve code efficiency is to apply the DRY principle: Don't Repeat Yourself. Functions allow you to do just that.
Functions group blocks of code and allow you to run that code as many times as you like by simply referencing its name as a function call. For example, myFunction(); triggers a function named myFunction. In their simplest form, functions can be compared to ant-hills.
- The entrance to the ant-hill is like the function call - a deceptively small gateway to a much larger hive full of action.
- The hive that lies below ground is like the function declaration - the place where the action happens, hidden from view.
- The ants themselves are like the individual lines of code in the function - individual parts contributing to a common result.
Variable scope refers to the fact that not all variables are available at all times. Although there are exceptions and ways around this, standard variables are considered local and access to them is restricted, unlike constants which are global by default. For example, a local variable that was defined outside of a function is not accessible from within a function. We can describe this limitation by taking some artistic liberty with the ant-hill analogy. We will imagine that there are several greedy ant colonies with ant-hills in the same area, even though they all eat the same food, they refuse to share with each other and survive on stockpiles in their hive. If functions were truly unable to interact with other parts of your code, they would be much less useful. This is where arguments and return values come into play.
Arguments are used to pass information to a function. To make this adjustment, we must first prepare the function to receive information by adding a variable between the parentheses of the function declaration. For example, function myFunction() { ... } would become function myFunction($myVariable) { ... } note that myVariable is local to myFunction an therefore accessible from within the function. Next we need to specify the information to be passed by adding it between the parentheses of our function call. For example, myFunction(); would become myFunction('example text'); where example text is the string being passed. It is possible to specify more than one argument by using a comma-separate list of variables and values in the function declaration and function call(s), respectively. For example, function myFunction($myVariable, $myOtherVariable) { ... } and myFunction('example text', 'other example text'); would allow $myVariable to receive example text and $myOtherVariable to receive other example text.
Return values are used to get information back from a function, often this is some sort of result such as a calculation, an error, a row from a datasource or a true/false evaluation. For example, function myFunction() { return 3; } would return the integer 3. This means that any time the function runs, the function call that triggered it would take on the return value. For example echo "The number is " . myFunction(); would display The number is 3. Note that PHP halts the execution of a function when it hits a return statement.
The usage of functions will be summarised in the following exercise.
Exercise
Our objective is to calculate and display the perimeter of 3 different circles using PHP. The end result should be similar to what you see on screen now.
We will start by opening the supporting file "circle_properties2.php" which has been set up as a static page. This file is identical to the end file from "circle_properties1.php" which was the previous exercise. First we will wrap the calculations in a function and specify a variable to receive the radius argument, then we will adapt the function to use the radius. Rather than echoing the result directly, we will change the function to return the result. Next we will create a call to the function and pass a value of 3 for the radius argument. Since we removed the echo from the function and had it return a value instead, we will need to echo the function call. Now that we are sure our function is working correctly, all we need to do is copy-paste the function call and pass a different value as the radius. Note that we were still able to use the constant PI inside the function because it is part of the global scope.
If you view your file through a local or live server that can process PHP, you should see the desired result.
Screencast URL: To come
No comments:
Post a Comment