How to pass parameters to a procedure

Source: Internet
Author: User
Tags expression integer

Code in the process usually requires some information about the state of the program to complete its work. Information includes variables that are passed to the procedure when the procedure is invoked. When a variable is passed to a procedure, the variable is called a parameter.
1. Data type of parameter
The parameters of the procedure are default to have the object data type, but you can also declare the parameter to be of another data type. For example, the following function accepts a string and an integer:

Function Whatsforlunch (Weekday as string,hour as Integer) as String
   ' returns lunch menu
   If weekday= ' Friday ' according to day of the week and time then< C4/>whatsforlunch= "Fish"
   Else
      whatsforlunch= "Chicken" End
   if
   if hour>4 Then whatsforlunch= "Too Late "End
Function

2. Passing parameters by value
Passing parameters by value is the default in vb.net. When you pass a parameter by value, only a copy of the variable is passed. If the procedure changes this value, the change affects only the copy and does not affect the variable itself. Use the "ByVal" keyword to indicate that the parameter is passed by value. For example:

Sub postaccounts (ByVal intacctnum as Integer)
  ' here put the statement end
Sub
Note: The ByVal keyword can be omitted.

3. Pass the parameter by address
Passing an argument by address is a procedure that accesses the contents of the actual variable with the memory address of the variable. As a result, when passing a variable to a procedure, the value of the variable can be changed by a procedure. Passing parameters by address if you specify a data type by address pass parameter, you must pass the value of this type to the argument. Visual Basic evaluates an expression and, if possible, passes the value to the parameter in the requested type. The easiest way to convert a variable to an expression is to put it in parentheses. Using the "BYREF" keyword to indicate that the parameter is passed by address, it is more efficient to pass the address because, regardless of the type of variable, the pass is only 4 bytes. The following example: The parameter "RunningTotal" is passed in by address, so the variable value that is passed in is the value of the parameter "Acctnum".

Sub Postaccount (ByVal acctnum as Integer,byref runningtotal as single)
   Runningtotal=acctnum end
Sub

4. Use optional parameters
By including the "Optional" keyword in the parameter list of a procedure, you can specify that the parameters of the procedure are optional. Specify optional parameters, based on the following three rules:
(1) Each optional parameter must have a default value;
(2) The default value of an optional parameter must be a constant;
(3) All parameters following optional parameters also need to be optional parameters
The following example gives the definition of a procedure with optional parameters:
Sub subname (Optional ByVal mycountry as string= "the")
....
End Sub
When this procedure is invoked, you can choose whether to pass parameters to the procedure, or the procedure uses the default parameters if the argument is not passed.
There may be times when you don't want to pass a value on an optional parameter, and you don't want to use the default value, but Visual Basic is not allowed, so you can solve the problem by setting an optional default value for the optional parameter, as in the following example:

Sub Placeinfo (ByVal state as String,optional ByVal Country as string= "QJX")
   If country= "QJZ" Then
      Debug.WriteLine ("Country not supplied-using Australia")
      country= "Australia" End
   If
   MsgBox ("The State of" &State& ' is in ' &country end
Sub

You can call this procedure this way:
Placeinfo (state:= "Maryland", country:= "USA")
'...
Placeinfo (state:= "Queensland")

5. Use indefinite number of parameters
In general, the number of parameters in a procedure call should equal the number of parameters described in the procedure. You can pass parameters to a procedure using an array of parameters, and when you define a procedure, you do not have to know the number of elements in the parameter array, and the size of the parameter array is determined by each call to the procedure.
The array parameters are represented by the keyword "ParamArray" with the following rules:
(1) A procedure can have only one parameter array, and the parameter array must be behind other parameters.
(2) The parameter array must be passed by value, and the keyword "ByVal" is explicitly defined when the procedure defines this parameter array.
(3) The parameter array must be a one-dimensional array, and each element of the parameter array itself must be of the same type and, if not defined, processed by object type.
(4) A parameter array once declared is an optional parameter, its default value is the empty value of each type.
The following example illustrates the use of a parameter array:

Sub Studentscores (Name as String,byval Parmarray Scores () as Object)
   Dim I as Integer
   Debug.WriteLine ("Scores for ' &Name& ': ' Use
   UBound function to determine largest subscript of array for
   i=0 to UBound (Scores ())
       Debug.WriteLine ("Score" &I& ":" &scores (I))
   Next I end
Sub
can call this procedure like this:
studentscores (" Jamie ", 10,26,32,15,22,24,16)
  ' ...
Studentscores ("Kelly", "High", "Low", "Average" and "high")

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.