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

  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

  





本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/295988.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python 字典操作

假设字典为dics {0:a, 1:b, c:3} 1.从字典中取值,当键不存在时不想处理异常 [方法] dics.get(key, not found) [例如] [解释] 当键key不存在是,打印not found(即想要处理的信息),当存在是输出键值。 【其他解决方案一】 if key in dics: …

Web实时通信,SignalR真香,不用愁了

前言对于B/S模式的项目,基础的场景都是客户端发起请求,服务端返回响应结果就结束了一次连接;但在很多实际应用场景中,这种简单的请求和响应模式就显得很吃力,比如消息通知、监控看板信息自动刷新等实时通信场景&#x…

四位数码管树莓派c语言,用树莓派和四位数码管模块做一个时钟

8种机械键盘轴体对比本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?预备知识如图,这是一个4位数码管模块,由两片 74HC595 级联控制,引脚如图,从上到下,分别为VCC(3.3V/5V),…

这5部不容错过的超高评价纪录片,带你领略地球的魅力!

全世界只有3.14 % 的人关注了爆炸吧知识地球是茫茫宇宙星系中唯一生机勃勃万物生存的星球,它是瞬息万变、充满自然奇观的世界。50多亿年来,地球在日复一日、沧海桑田地变化着。今天就给大家带来最顶尖的BBC地球科普纪录片,最盛大的听觉盛宴&a…

【转】Android 带checkbox的listView 实现多选,全选,反选 -- 不错

原文网址:http://blog.csdn.net/onlyonecoder/article/details/8687811 Demo地址(0分资源):http://download.csdn.net/detail/onlyonecoder/5154352 由于listview的一些特性,刚开始写这种需求的功能的时候都会碰到一些…

第三方QQ、微博登陆

一定要把token uid 这些参数搞明白是干什么的 第一种方法是转的 &#xff08;通过javascript来调用&#xff09; 最近接触了一些第三方登陆的东西&#xff0c;弄的真是一个头&#xff0c;两个大>.< 今天&#xff0c;终于把腾讯第三方登陆调试通了&#xff0c;做一…

js时间-价格-排序案例____冒泡排序实例

js 时间-价格-排序&#xff0c;静态页面排序案例冒泡排序实例 在项目中用到的一个例子&#xff0c;分享一下。 var sort {sortdata : , //排序之前必须先填充原始数据 //来源我的博客&#xff1a;http://yijianfengvip.blog.163.com/blog/static/1752734322011102145823/…

Horspool 字符串快速查找算法

Horspool算法是后缀搜索算法&#xff0c;对于每个文本搜索窗口&#xff0c;将窗口内的最后一个字符与模式串(needle)的最后一个字符进行比较。如果相等&#xff0c;则继续从后向前验证其他字符&#xff0c;直到完全相等或者某个字符不匹配。当遇到字符不匹配的情况时就需要将搜…

由NTC电阻值 计算温度 C语言,高精度NTC测温电路设计及电阻值计算

什么是NTCNTC是热敏电阻&#xff0c;其电阻值对温度变化敏感&#xff0c;在不同的温度下&#xff0c;可以呈现不同的电阻值。热敏电阻有两类&#xff0c;一类是负温度系数电阻(NTC)&#xff0c;温度增加时&#xff0c;电阻值降低&#xff0c;另一类是正温度系数电阻(PTC)&#…

史上最被低估的两个学科!它们远比你想的更重要!

▲点击查看哈佛大学本杰明教授曾说&#xff1a;“越是到了高等教育的阶段&#xff0c;人们就越重视从历史中总结经验&#xff0c;尤其是精英阶层。很多人都想好好读读历史&#xff0c;但是一直以来&#xff0c;读历史都有一个问题&#xff1a;看着满满都是字的大部头&#xff0…

c#10:string内插处理

string内插是指用$"日期&#xff1a;{DateTime.Now.ToString("yyyy年MM月dd日")}。"&#xff0c;这种使用方式&#xff0c;在c#10.0时&#xff0c;引进了这种string内插的处理&#xff0c;可以写自定义的处理程序&#xff0c;来提升使用体验。具体用法见下…

Application Fundamentals

Application Fundamentals 署名&#xff1a;译言biAji 链接&#xff1a;http://developer.android.com/guide/topics/fundamentals.html 应用程序基础(Application Fundamentals) Android应用程序使用Java做为开发语言。aapt工具把编译后的Java代码连同其它应用程序需要的数据…

Android之玩转选项卡(TabHost、TabWidget、FrameLayout)

选项卡(TabHost、TabWidget、FrameLayout) 选项卡由TabHost、TabWidget、FrameLayout 这3个组件构成,用于实现一个多标签页的用户界面,不费话了,先爆图,就知道是什么鬼了,怎么用了。 第一步 配置activity_main.xml <TabHost xmlns:android="http://schemas.andro…

c语言扑克牌随机发三张牌,扑克牌发三张概率组合一副扑克牌52张(不含大、小王),发三张牌,一共多少种组合.另外:1)三张中含一对的组合有多少张?(...

共回答了20个问题采纳率&#xff1a;95%这个题目我喜欢所有组合为&#xff1a;C(52,5)1、A组为三张不关联明牌,组合&#xff1a;52*48*44,与B组二张暗牌组成一个对子.B的组合为&#xff1a;(12-3)*(52-12).概率&#xff1a;(12-3)*(52-12)*2/C(49,2)30.6%组成两个对子的概率3*3…

iOS使用NSURLConnection发送同步和异步HTTP Request

1. 同步发送- (NSString *)sendRequestSync{// 初始化请求, 这里是变长的, 方便扩展NSMutableURLRequest *request [[NSMutableURLRequest alloc] init];// 设置[request setURL:[NSURL URLWithString:urlStr]];[request setHTTPMethod:"POST"];[request setValue:h…

请设计各种管理系统、业务系统的大哥大姐们,设计新系统时,拜托您,请允许我修改用户名、密码...

大概在2-3年前&#xff0c;我在宁波最大的一家IT公司工作&#xff0c;我们公司日常开发人员经常会使用的系统有很多&#xff0c;夸张的 说接近10个吧&#xff0c;有些是自己公司开发的&#xff0c;有些是成熟的商品化软件&#xff0c;这些系统又由不同的维护人员维护&#xff0…

这是你想象中的泳池美女吗?爱了吗?

1 小手一插&#xff0c;谁都不爱▼2 玩滑板讲究顺其自然哪里摔倒&#xff0c;就在哪里跪下▼3 陪老婆待产的企鹅们▼4 这就是米其林三星级的地摊吗&#xff1f;▼5 年度最好的送男友礼物▼6 张雨绮表演内心戏全程▼7 说&#xff01;这种极品西瓜哪里有卖&#xff1f;▼你…

如何快速学习使用mybatis以及总结

第一步 让我来介绍下初学者怎么快速更好的使用mybatis这个框架 使用mybatis的话&#xff0c;每个Dao就对于一个相应的xml文件&#xff0c;我来给个例子个大家看&#xff0c;先要配置好环境。在application.xml里面<!-- c3p0 connection pool configuration 数据库的配置 --…

Abp Vnext Vue3 的版本实现

Abp Vnext Pro 的 Vue3 实现版本 开箱即用的中后台前端/设计解决方案开始Github地址文档地址演示地址系统功能[x] 用户管理[x] 角色管理[x] 审计日志[x] 后台任务[x] 集成事件[x] IdentityServer4[x] 客户端管理[x] Api 资源管理[x] ApiScope 管理[x] Identity 资源管理[x] Sin…

递归函数就兔子数C语言,【C语言】求斐波那契(Fibonacci)数列通项(递归法、非递归法)...

意大利的数学家列昂那多斐波那契在1202年研究兔子产崽问题时发现了此数列&#xff0e;设一对大兔子每月生一对小兔子&#xff0c;每对新生兔在出生一个月后又下崽&#xff0c;假若兔子都不死亡&#xff0e;问&#xff1a;一对兔子&#xff0c;一年能繁殖成多少对兔子&#xff1…