Home Backend Development PHP Problem You must understand the parameters of the function (in detail)

You must understand the parameters of the function (in detail)

Jun 03, 2021 pm 02:37 PM
parameter

The last article introduced you to "Let’s talk about the difference between the return statement and echo (detailed explanation and examples)". This article continues to introduce the parameters of the function to you. Now let’s go and take a look. Look! ! !

You must understand the parameters of the function (in detail)

Parameters of function (actual parameters and formal parameters):

Formal parameter variables only allocate memory units when called , at the end of the call, the allocated memory unit is released immediately. Therefore, formal parameters are only valid within the function. After the function call ends and returns to the calling function, the formal parameter variable can no longer be used.

Actual parameters can be constants, variables, expressions, functions, etc. No matter what type of quantities the actual parameters are, they must have definite values ​​when making function calls so that these values ​​can be transferred to formal parameters. Therefore, assignment, input, etc. should be used in advance to obtain a certain value for the actual parameters.

The actual parameters and formal parameters should be strictly consistent in number, type, and order, otherwise a "type mismatch" error will occur.

The data transfer that occurs in function calls is one-way. That is, only the value of the actual parameter can be transferred to the formal parameter, but the value of the formal parameter cannot be transferred in the reverse direction to the actual parameter. Therefore, during the function call, the value of the formal parameter changes, but the value of the actual parameter does not change.

Simply put, parameters are divided into two parts: formal parameters and actual parameters:

Formal parameters are formal parameters, used in parentheses when defining functions;

The actual parameter is the actual parameter, used in the parentheses of the calling function;

For example, int F(int i) i is the formal parameter,
The actual parameter is the function call When there are parameters passed by the calling function to the called function, for example:

1

2

int x=1;

a=F(x);

Copy after login


x here is the actual parameter. After the function is called, the value of x is passed to the formal parameter. i

Note:

  • If the function has defined parameters, the corresponding actual parameters must be passed in when calling the function (When the formal parameter has no default value)

  • If the function also has the default value of the formal parameter and the corresponding actual parameter is passed in, then the passed actual parameter will be the main one (equivalent to For variable assignment, use the value of the actual parameter to assign the value to the formal parameter)

The specific operation takes the code as an example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?php

     /******函数的参数 */

     function table (){

         $str = &#39;<table border="1" align="center" width="800">&#39;;

         for ($tr = 0; $tr < 10; $tr ++){

             $str .= &#39;<tr>&#39;;

             for($td = 0; $td < 10; $td ++){

                $str .= &#39;<td>&#39;.$tr.$td. &#39;</td>&#39;;

            }

            $str .= &#39;</tr>&#39;;

         }

         $str .= &#39;</table>&#39;;

         //返回值  返回一个表格

         return $str;

     }

     $table = table();

     echo $table;

?>

Copy after login

The demonstration results are as follows:

You must understand the parameters of the function (in detail)Code analysis:

We use the function function to define a table, and then set the specifications of the table (supplementary: align refers to adjustment, calibration, center: center), and then set Define the for loop, enter the for loop and continue to set the second for loop statement. The specific code is as shown above. At the end, we use return to return the value (the purpose is to determine whether the return statement can return a value successfully and whether it can return a table. ), when we return a table, call the table statement, {$table=table()}, output (table). Overall, we first define a variable (table), connect one (tr tag) in the table, and then connect 10 (td). At this time, the loop ends, and we connect an end flag (/td). At this time, we continue Loop, and so on;

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of You must understand the parameters of the function (in detail). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot tools Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New feature in PHP version 5.4: How to use callable type hint parameters to accept callable functions or methods New feature in PHP version 5.4: How to use callable type hint parameters to accept callable functions or methods Jul 29, 2023 pm 09:19 PM

New feature in PHP version 5.4: How to use callable type hint parameters to accept callable functions or methods

i9-12900H parameter evaluation list i9-12900H parameter evaluation list Feb 23, 2024 am 09:25 AM

i9-12900H parameter evaluation list

What do product parameters mean? What do product parameters mean? Jul 05, 2023 am 11:13 AM

What do product parameters mean?

C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument Sep 17, 2023 am 10:49 AM

C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument

PHP Warning: Solution to in_array() expects parameter PHP Warning: Solution to in_array() expects parameter Jun 22, 2023 pm 11:52 PM

PHP Warning: Solution to in_array() expects parameter

C++ function parameter type safety check C++ function parameter type safety check Apr 19, 2024 pm 12:00 PM

C++ function parameter type safety check

Summary of machine learning hyperparameter tuning (PySpark ML) Summary of machine learning hyperparameter tuning (PySpark ML) Apr 08, 2023 pm 07:21 PM

Summary of machine learning hyperparameter tuning (PySpark ML)

Analysis of common parameters and usage of numpy functions Analysis of common parameters and usage of numpy functions Jan 26, 2024 am 08:17 AM

Analysis of common parameters and usage of numpy functions

See all articles