Passing VB Function Parameters

Source: Internet
Author: User

In fact, the complete syntax format of the form parameter should be:

[[Optional] [byval | byref] | paramarray] <variable name> [()] [as <type>] [= <default value>]

Byval indicates that this parameter is passed by value, or a value parameter for short. In this case, each of the input parameters occupies an independent bucket, which is allocated only when called. If a value parameter is selected, the operation in the real machine is irrelevant, and the original value is retained. Therefore, to protect real parameters from the impact of procedural operations, numerical parameters should be selected.

Byref indicates that the parameter is passed by address, which is called a variable parameter. In this case, the bucket that participates in the real parameter occupies the same bucket, and the bucket of the real parameter is allocated before the call. If you select a variable parameter, the actual operation is related to the process. Changes to the shape parameter will affect the value of the variable parameter. Therefore, when two or more values need to be transferred to the caller during the call process, address transmission should be selected. Since the variable parameter is the default value passing method of VB, we used variable parameters in many places before that.

It should be noted that if the real parameter is a constant, it is a value parameter.

Value Transfer: Pass the value. After passing the parameter, the form parameter and the variable parameter are separated from the Link parameter, which may change but does not bring the real parameter. This is a one-way transfer ". In this case, the form parameter is generally a variable, and the real parameter can be a constant, variable, or expression. The parameter memory is used for calling.

Value Transfer: Pass the value. After passing the parameter, the form parameter and the variable parameter are separated from the Link parameter, which may change but does not bring the real parameter. This is a one-way transfer ". In this case, the form parameter is generally a variable, and the real parameter can be a constant, variable, or expression. The parameter memory is used for calling.

Address Transfer: the transfer address. If the real parameter is used by the same address, the change of the form parameter will directly bring the real parameter, which is a "two-way transfer ". In this case, the form parameter and real parameter are generally variables or arrays.

Option base 1

Private sub commandementclick ()

Dim A (3) as single, B (3) as single, C (3) as single

Dim X as single, y as single

Dim I as integer

For I = 1 to 3

A (I) = Val (inputbox ("Enter the coefficient of the" & I & "equation A:", "input box "))

B (I) = Val (inputbox ("Enter the coefficient B for the" & I & "equation:", "input box "))

C (I) = Val (inputbox ("Enter the coefficient C of the" & I & "equation:", "input box "))

Call root (a (I), B (I), C (I), x, y)

Print "th"; I; "equations"; a (I); "x ^ 2 +"; B (I); "x +"; C (I ); "= 0 root :"

Next I

Print X, Y

Call oneline

End sub

Public sub root (byval A as single ,_

Byval B as single, byval C as single ,_

X1 as single, X2 as single) // The default value of VB is variable parameters.

Dim D as single

Dim P as single, Q As single

D = B * B-4 * a * C

P =-B/2/A: q = sqr (d)/2/

X1 = p + q: X2 = p-Q

End sub

Public sub oneline ()

Dim K as integer

For k = 1 to 40

Print "= ";

Next K

Print

End sub

 

Note:

(1) A, B, C, X1, and X2 are form parameters, and a (I), B (I), C (I), X, and Y are real parameters.

(2) where A, B, and C are value parameters, and X1 and X2 are variable parameters.
Similarities and differences between byval and byref passed by functions
-------------------------------------

First of all, the results obtained by the two methods are uncertain and certainly the same.

Passing a parameter through byref changes the value of the parameter, but byval does not, because byval only transmits the copy of the variable to the function.

So what is the impact of passing parameters in these two methods on the code efficiency? I think this is what most programmers care about.

By default, VB transmits parameters by byref. In general, the byval mode is faster than the byref method, but when you want to pass a large string or array, byref is faster than byval, because byref only needs to pass the four-byte pointer to the data, rather than the actual data. therefore, when performing an application, you can consider the actual situation for processing to achieve the best running speed.

Digress:

Long is the fastest variable type in VB.
If you do not need to retain decimal places during operations, do not use floating-point operations as far as possible, because integer operations do not require a digital coprocessor.
Avoid using object attributes instead of constants for calculation. Access to any object attribute will affect the operation speed.
Try to use dynamic arrays instead of static arrays, because the erase method can clear unnecessary elements in the dynamic array and release occupied memory. For static arrays, only the elements in the array can be cleared, and the memory used by the array elements will not be released.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.