What is parameter passing? What are the parameters passed?

table of Contents

What is the process?

What is the transfer of parameters?

What happens if the data types of the passed parameters do not match?

What is the parameter passed?

Delivery by address (ByVal)

Pass by address (ByRef, default)


 

What is the process?

Execute the same program code


What are the processes?

Sub

Function

Subroutine

function

Call statement call

Call statement call

No return value

Has return value


Why use process? What are the benefits?

Avoid code verbosity and prevent the code from being written as if it were written. There are instructions in the previous blog, so I won’t repeat them here.


What is the syntax format of Sub procedure and Function procedure respectively?

[Private|Public][Static] Sub < procedure name> ([parameter list])

          Process body

          [Exit Sub]

End Sub

[Private|Public|Static] Function function name ([Parameter table] [AS type])

      [Function Body]

      [<function name>=<expression>]

      [Exit Function]

End Function

Description:

The process must comply with the principle of "explain first, call later" . The statement process is to write a program segment that can achieve the expected function, which can be called repeatedly.

The naming rules of the procedure name are the same as that of the variables. The procedure name must be unique and must not exceed 255 characters

The parameter table is used to indicate the parameters and types that need to be passed to the procedure when calling the Sub procedure. The parameters in the parameter table are also called formal parameters (referred to as formal parameters). There can be no formal parameters, or one or more formal parameters.

The naming rules of formal parameters are the same as that of variables. Formal parameters have data types. If the "AS type" is omitted, the type will not be explained, and the default is Variant type

The formal parameter itself is a name, it does not take up memory space , and only participates in calculations and assignments after being called by the function, and takes up memory space

The parameters in the function parameter list are called formal parameters, and the values ​​given when the function is called are called actual parameters


 

What is the transfer of parameters?

When the procedure is called, the actual parameter passes the parameter value to the corresponding formal parameter, and then realizes the process of data processing and return result in the process


What are the rules for parameter passing?

Equal number, compatible data type, consistent order


What happens if the data types of the passed parameters do not match?

Example: in the following code

When the program runs, it will report an error:

Reason : The data types of actual parameters and formal parameters do not match,

Note: In the redefined max process, the formal parameters m and n correspond to the data type of Integer (integer), and its range is positive integer, negative integer or 0,

In the process of calling max, the data type of the actual parameters d and c at the time of definition is Single (single precision), and the range is a real number with decimals.

Their data types contain different ranges. It is like, I want to install a big watermelon, but you give me a small bowl. Can that small bowl hold the next big watermelon? The answer is absolutely no.


What is the parameter passed?

You can pass constants, variables, expression calculation results, and arrays. This is related to the delivery method to be discussed next


What are the delivery methods?

Delivery by address ( ByVal )

When calling, the system sends the value of the actual parameter to the formal parameter, and the formal parameter is disconnected after receiving the value. Any operations on the formal parameters in the procedure body will not affect the actual parameters

For example: I have a book that I lent to Xiao Li through Xiao Ming. What Xiao Li actually got is a copy of my book. No matter how Xiao Li modifies this book, it will not affect me.

Examples:

The execution results are shown in the figure:


Pass by address ( ByRef , default)

When calling, the address of the actual parameter is passed to the formal parameter, the actual parameter and the formal parameter share the storage unit, and any operation on the formal parameter in the procedure body becomes the operation of the corresponding actual parameter

For example, there is a book for actual parameters, and the book is given to the formal parameters. Instead of a copy, the key to the room where the book is installed is given. Both the actual parameters and the formal parameters can share it, because they both store this book. The key of the book, you can use it if you have the address, the formal parameters can be modified at will when you get the book, the original is the original

Examples:

'''''''''''''''''
'输出X的值

Private Sub Command1_Click()
    Dim x As Integer
    x = 6
    Call test(x)
    Print "    sub   ̺ x="; x
End Sub



Sub test(ByRef m As Integer)
    m = m + 5
End Sub

Guess you like

Origin blog.csdn.net/weixin_43319713/article/details/106116690