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

  

从函数中返回多个值的方法相关推荐

  1. bash shell函数中返回任意值的四种方法

    From: http://www.jbxue.com/article/11322.html 本文介绍下,在bash shell编程中,从函数中返回任意值的几种方法,有需要的朋友参考下. 在bash中, ...

  2. C函数实现返回多个值的方法

    C语言中,一个函数最多只能实现一个返回值. int func (int b) { int a=5; if (a>b) return a; else return b; return 0; } 通 ...

  3. 函数中参数有数组时注意的小问题(不一定要传递数组长度,不用返回数组,可以在函数中改变数组元素值)

    函数中参数有数组时注意的小问题: 1.不一定要传递数组长度 2.不用返回数组,可以在函数中改变数组元素值 通过下面这个小例子来验证: #include<iostream> #include ...

  4. 错误 未找到引用源_你不理解的EXCEL函数中常见的错误值,都在这里

    今天我们来聊聊EXCEL函数中常见的错误值 使用EXCEL一定会用到函数公式,,使用函数公式计算分析数据,不免会遇到出错的情况, 但是出错后不知道错误原因,为什么会返回错误值,所以你的函数还不能自己优 ...

  5. java map 多个值_java 一个函数EnumMap返回多个值

    在开发过程中,经常会有这种情况,就是一个函数需要返回多个值,这是一个问题!! 网上这个问题的解决方法: 1.使用map返回值:这个方法问题是,你并不知道如何返回值的key是什么,只能通过doc或者通过 ...

  6. 《C++面向对象高效编程(第2版)》——3.16 从函数中返回引用

    本节书摘来自异步社区出版社<C++面向对象高效编程(第2版)>一书中的第3章,第3.16节,作者: [美]Kayshav Dattatri,更多章节内容可以访问云栖社区"异步社区 ...

  7. 在JavaScript中返回多个值?

    我试图在JavaScript中返回两个值. 那可能吗? var newCodes = function() { var dCodes = fg.codecsCodes.rs;var dCodes2 = ...

  8. 【Java方法】从方法中返回多个值

    [Java方法]从方法中返回多个值 (1)java方法只能返回一个值,但是在实际需求中经常遇到一个方法返回多个值,这是该如何操作? 思路:可以量多个值装到字典(Map)中,将字典作为一个值返回. (2 ...

  9. Python函数返回多个值的方法

    如果程序需要有多个返回值,则既可将多个值包装成列表之后返回,也可直接返回多个值.如果 Python 函数直接返回多个值,Python 会自动将多个返回值封装成元组. 如下程序示范了函数直接返回多个值的 ...

最新文章

  1. 周伯文对话斯坦福AI实验室负责人:下一个NLP前沿是什么?
  2. 计算机软考中集成系统,软考中级系统集成项目管理工程师有哪些作用?
  3. markdown 本地链接_markdown多平台发布及七牛图床使用
  4. Cocos2d-x3.2 Menu菜单的创建
  5. [React-Native]环境配置amp;HelloWorld
  6. vivo android 刷机教程,vivo Xshot升级Android 4.4刷机教程
  7. dosbox 实现程序编译
  8. 实习日记(4-28)
  9. 计算机主机的拆卸步骤,电脑清灰教程:电脑主机怎么清理灰尘?台式电脑主机清理灰尘教学...
  10. oneNET连接配置参数说明
  11. 解决联想小新笔记本电脑触摸板失灵
  12. 面试官:怎么不用定时任务实现关闭订单?
  13. 计算机excel基础知识教程,EXCEL基本操作技巧 一
  14. 传统产业如何在互联网时代创新与转型
  15. 【动手学深度学习v2李沐】学习笔记07:权重衰退、正则化
  16. mysql 统计 打卡数据_第九期30天打卡赠书和红包活动,今天正式启动!
  17. ASCII表与字符编码
  18. OpenCV系列之霍夫圈变换 | 三十三
  19. kali突然连不上网络的解决方案
  20. 没有万能的结构和选择

热门文章

  1. Google,真的要离我们而去吗?
  2. 破解MS Word 的只读密码限制
  3. 智能家居 (8) ——智能家居项目整合(网络控制线程、语音控制线程,火灾报警线程)
  4. python默认参数只被解释一次_深入讲解Python函数中参数的使用及默认参数的陷阱...
  5. 2 什么是计算机网络设置密码,计算机网络技术及应用(第2版)第6章网络操作系统基本配置.ppt...
  6. 分布式自增ID算法---雪花算法(SnowFlake)Java实现
  7. 福禄克网络TIA识别MPTL连接的操作方法
  8. 如何构建一个向导操作模式程序
  9. apache zookeeper java_zookeeper启动报错java.net.NoRouteToHostException
  10. 数据挖掘相关知识介绍