日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

从函数中返回多个值的方法

發布時間:2023/12/9 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从函数中返回多个值的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  There are several ways to return multiple values from functions. In this topic, we’re going to look over the 5 most common techniques to pass 2 or more values from functions. The 5 techniques are:

  1、Returning variables in Global scope

  2、Returning a Collection

  3、Returning Arrays

  4、Using Concatenated Strings

  5、Passing through the use of ByRef

  Returning variables in Global scope
  This can be achieved by declaring the variables outside the scope of the function. Here, we don’t need to pass the values through the function; but we can simply manipulate them within the function’s scope. Please note that if the same string is declared within the function, it loses its global scope – as it becomes local to the function. These variables can come from a function library or from the test script as long as they are outside the scope of the calling method. Code snippet:  

Dim intNumber_1: intNumber_1 = 40 Dim intNumber_2: intNumber_2 = 80Public Sub PassValuesintNumber_1 = intNumber_1/4intNumber_2 = intNumber_2/4 End SubPassValuesMsgBox "intNumber_1 = " & intNumber_1 &_vbLf & "intNumber_2 = " & intNumber_2

  Returning a Collection

  Another way to pass multiple values from a function is through the using of creating and passing Collections. We can use a collection object to store multiple values as keys/items. Code snippet:

Public Function PassValues(ByVal Num_1, ByVal Num_2)Set oDict = CreateObject( "Scripting.Dictionary" )With oDict.Add "Num_1", Num_1/4.Add "Num_2", Num_2/2End WithSet PassValues = oDict End FunctionSet colNumbers = PassValues(40,80)MsgBox "intNumber_1 = " & colNumbers.Item("Num_1") &_vbLf & "intNumber_2 = " & colNumbers.Item("Num_2")

  Returning Arrays

  This is quite a common technique. Each element in the array stores a variable that is then passed through the function. Code snippet:

Public Function PassValues(ByVal Num_1, ByVal Num_2)Dim arrArray: ReDim arrArray(2)arrArray(0) = Num_1/4arrArray(1) = Num_2/2PassValues = arrArray End FunctionarrNew = PassValues(40,80)MsgBox "intNumber_1 = " & arrNew(0) &_vbLf & "intNumber_2 = " & arrNew(1)

  Concatenated Strings

  I have seen the usage of this technique almost as frequently as the use of arrays. Here, two or more concatenated numbers/strings can be passed through the function with the help of a delimiter. Code snippet:

Public Function PassValues(ByVal Num_1, ByVal Num_2)Num_1 = Num_1/4Num_2 = Num_2/2PassValues = Num_1 & "," & Num_2 End FunctionsNum = PassValues(40,80)MsgBox "intNumber_1 = " & Split(sNum, ",")(0) &_vbLf & "intNumber_2 = " & Split(sNum, ",")(1)

  Using ByRef to Pass Multiple Values

  Please refer to the article?Passing Parameters ByRef and ByVal?for a detailed explanation of this technique. It can be used to pass multiple values in the following manner:

Dim intNumber_1: intNumber_1 = 40 Dim intNumber_2: intNumber_2 = 80Public Sub PassValues(ByRef Num_1, ByRef Num_2)Num_1 = Num_1/4Num_2 = Num_2/2 End SubPassValues intNumber_1, intNumber_2MsgBox "intNumber_1 = " & intNumber_1 &_vbLf & "intNumber_2 = " & intNumber_2

  





總結

以上是生活随笔為你收集整理的从函数中返回多个值的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。