本文主要是介绍VBA Brush Up 04:Passing Arguments to Procedures and Functions,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
来自:Julitta Korol,“Access.2003.Programming.by.Example.with.VBA.XML.and.ASP”,by Wordware Publishing, Inc. 2005, p52-p75
- Once the function does something, the result is assigned to the function name. Notice that the function procedure’s name is used as if it were a variable.
- By default, Visual Basic passes information to a function procedure (or a subroutine) by reference (ByRef keyword), referring to the original data specified in the function’s argument at the time the function is called. So, if the function alters the value of the argument, the original value is changed.If you want the function procedure to change the original value, you don’t need to explicitly insert the ByRef keyword, because passed variables default to ByRef.
When you use the ByVal keyword in front of an argument name, Visual Basic passes the argument by value. It means that Visual Basic makes a copy of the original data. This copy is then passed to a function. If the function changes the value of an argument passed by value, the original data does not change — only the copy changes - Arguments that are optional come at the end of the argument list, following the names of all the required arguments. Optional arguments must always be the Variant data type. This means that you can’t specify the Optional argument’s type by using the As keyword.
- The IsMissing function allows you to determine whether or not the optional argument was supplied. This function returns the logical value of True if the third argument is not supplied, and returns False when the third argument is given.
- Function Avg(num1, num2, Optional num3)
- Dim totalNums As Integer
- totalNums = 3
- If IsMissing(num3) Then
- num3 = 0
- totalNums = totalNums - 1
- End If
- Avg = (num1 + num2 + num3) / totalNums
- End Function
- Arguments vs. Parameters
• An argument is a variable, constant, or expression that is passed to a subprocedure.
• A parameter is a variable that receives a value passed to a subprocedure.
这篇关于VBA Brush Up 04:Passing Arguments to Procedures and Functions的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!