The function displays the following message:. The terms parameters and arguments are often used interchangeably. When you define a function that accepts inputs, you specify the parameters. An argument is a piece of data that you pass into the function when you call it. In the following function call, the literal string 'Admin' is an argument:. A function can return a value. To return a value from a function, you use the return statement:.
The return statement immediately ends the execution of the current function and returns the value. The value can be a literal value like a number and a string. Also, it can be a variable or an expression. Typically, a function contains only PHP code. On the other hand, it is more error prone. Which way of passing arguments should we use? It depends on the situation. Say we have a set of data, salaries of employees.
If we want to compute some statistics of the data, we do not need to modify them. We pass by values. If we work with large amounts of data and the speed of computation is critical, we pass by reference. If we want to modify the data, e. The original variables are not affected. We call the swap function. Inside the swap function, we change the values. They are valid only inside the swap function. The next code example passes values to the function by reference. The original variables are changed inside the swap function.
Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition. In other words, a recursive function calls itself to do its job. Recursion is an alternative to iteration. Recursion is the main approach in functional languages. Inside the body of the factorial function, we call the factorial function with a modified argument.
The function calls itself. Next we will talk about the scope of the variables in PHP. A scope is the range in which a variable can be referenced. When we work with functions, there are two basic scopes: the global and the local scope. The local scope is also called a function scope. In this example, we have two variables with the same name. They do not collide because they exist in different scopes. Here are some advantages of using functions:.
The declaration of a user-defined function start with the word function , followed by the name of the function you want to create followed by parentheses i.
Note: A function name must start with a letter or underscore character not with a number, optionally followed by the more letters, numbers, or underscore characters. Function names are case-insensitive. You can specify parameters when you define your function to accept input values at run time. The parameters work like placeholder variables within a function; they're replaced at run time by the values known as argument provided to the function at the time of invocation.
You can define as many parameters as you like. However for each parameter you specify, a corresponding argument needs to be passed to the function when it is called. The getSum function in following example takes two integer values as arguments, simply add them together and then display the result in the browser. Tip: An argument is a value that you pass to a function, and a parameter is the variable within the function that receives the argument. However, in common usage these terms are interchangeable i.
As you can see the third call to customFont doesn't include the second argument. A function can return a value back to the script that called the function using the return statement. The value may be of any type, including arrays and objects.
0コメント