精华内容
下载资源
问答
  • vbs(asp)下的Function 语句
  • VBS中Sub与Function的区别
  • VBS基础篇 - 过程(sub 与 Function) 在VBscript中,有两种procedure:Sub procedure与Function procedure Sub过程:是包含在Sub和End Sub语句之间的一组VBScript语句。如果Sub过程无任何参数,则...

     在VBscript中,有两种procedure:Sub procedure与Function procedure

    Sub过程是包含在 Sub 和 End Sub 语句之间的一组 VBScript 语句。如果 Sub 过程无任何参数,则 Sub 语句必须包含空括号 ()

    Call GetName() '调用Sub过程
    Sub GetName()
        MsgBox "John" '输出字符串
    End Sub
    
    'Sub 过程可以使用参数(由调用过程传递的常数、变量或表达式)。实例代码如下:
    
    Call GetName("John") '调用Sub
    Sub GetName(name)
        MsgBox name '输出字符串
    End Sub
    
    '注意:Sub过程是没有返回值

     

     Function 过程:是包含在 Function 和 End Function 语句之间的一组 VBScript 语句。Function 过程与 Sub 过程类似,如果 Function 过程无任何参数,则 Function 语句必须包含空括号 ()

    Call GetName() '调用Function
    Function GetName()
        MsgBox "John" '输出字符串
    End Function
    
    'Function 过程可以使用参数(由调用过程传递的常数、变量或表达式)
    Call GetName("John") '调用Function Function GetName(name) MsgBox name '输出字符串 End Function 'Function 是有返回值的,通过函数名返回一个值,这个值是在过程的语句中赋给函数名的。Function 返回值的数据类型总是Variant getName "John" '调用Function Function getName(name) getName = name '设置返回值 End Function

     

     如何调用过程

    调用 Function 过程时,函数名必须用在变量赋值语句的右端或表达式中。

    调用 Sub 过程时,只需输入过程名及所有参数值,参数值之间使用逗号分隔。不需使用 Call 语句,但如果使用了此语句,则必须将所有参数包含在括号之中。

    下面的示例显示了调用过程的两种方式。一种使用 Call 语句;另一种则不使用。两种方式效果相同。

    getName "John" '调用过程的第一种写法
    Call getName("Candy") '调用过程的第二种写法
    Sub getName(name)
        MsgBox "我是"&name
    End Sub

     

    Sub与Function的区别:

    Sub没有返回值,而Function有返回值。

    Sub不能放在表达式中,而Function可以。

     

    posted on 2016-08-10 21:35 Ethon 阅读(...) 评论(...) 编辑 收藏

    转载于:https://www.cnblogs.com/wakey/p/5758967.html

    展开全文
  • 在 VBScript 中,过程被分为两类:Sub 过程和 Function 过程,需要的朋友可以参考下
  • 对于Sub程序和Function程序在安装QuickTest Professional后可以查看它的文档,QuickTest ProfessionalHelp里面也有关于VBscrīpt Procedures的说明:VBscrīpt ProceduresIn VBscrīpt, there are two kinds of ...

    对于Sub程序和Function程序

    在安装QuickTest Professional后可以查看它的文档,QuickTest Professional Help里面也有关于VBscrīpt Procedures的说明:

    VBscrīpt Procedures

    In VBscrīpt, there are two kinds of procedures; theSubprocedure and theFunctionprocedure.

    Sub Procedures

    A Sub procedure is a series of VBscrīpt statements (enclosed by Sub and End Sub statements) that perform actions but don't return a value. A Sub procedure can take arguments (constants, variables, or expressions that are passed by a calling procedure). If a Sub procedure has no arguments, its Sub statement must include an empty set of parentheses ().

    The following Sub procedure uses two intrinsic, or built-in, VBscrīpt functions,MsgBoxandInputBox, to prompt a user for information. It then displays the results of a calculation based on that information. The calculation is performed in a Function procedure created using VBscrīpt. The Function procedure is shown after the following discussion.Sub ConvertTemp()

    temp = InputBox("Please enter the temperature in degrees F.", 1)

    MsgBox "The temperature is " & Celsius(temp) & " degrees C."

    End Sub

    Function Procedures

    A Function procedure is a series of VBscrīpt statements enclosed by the Function and End Function statements. A Function procedure is similar to a Sub procedure, but can also return a value. A Function procedure can take arguments (constants, variables, or expressions that are passed to it by a calling procedure). If a Function procedure has no arguments, its Function statement must include an empty set of parentheses. A Function returns a value by assigning a value to its name in one or more statements of the procedure. The return type of a Function is always a Variant.

    In the following example, the Celsius function calculates degrees Celsius from degrees Fahrenheit. When the function is called from the ConvertTemp Sub procedure, a variable containing the argument value is passed to the function. The result of the calculation is returned to the calling procedure and displayed in a message box.Sub ConvertTemp()

    temp = InputBox("Please enter the temperature in degrees F.", 1)

    MsgBox "The temperature is " & Celsius(temp) & " degrees C."

    End Sub

    Function Celsius(fDegrees)

    Celsius = (fDegrees - 32) * 5 / 9

    End Function

    Getting Data into and out of Procedures

    Each piece of data is passed into your procedures using an argument . Arguments serve as placeholders for the data you want to pass into your procedure. You can name your arguments any valid variable name. When you create a procedure using either the Sub statement or the Function statement, parentheses must be included after the name of the procedure. Any arguments are placed inside these parentheses, separated by commas. For example, in the following example,fDegreesis a placeholder for the value being passed into the Celsius function for conversion.Function Celsius(fDegrees)

    Celsius = (fDegrees - 32) * 5 / 9

    End Function

    To get data out of a procedure, you must use a Function. Remember, a Function procedure can return a value; a Sub procedure can't.

    Using Sub and Function Procedures in Code

    A Function in your code must always be used on the right side of a variable assignment or in an expression. For example:Temp = Celsius(fDegrees)

    -or-MsgBox "The Celsius temperature is " & Celsius(fDegrees) & " degrees."

    To call a Sub procedure from another procedure, type the name of the procedure along with values for any required arguments, each separated by a comma. TheCallstatement is not required, but if you do use it, you must enclose any arguments in parentheses.

    The following example shows two calls to theMyProcprocedure. One uses the Call statement in the code; the other doesn't. Both do exactly the same thing.Call MyProc(firstarg, secondarg)

    MyProc firstarg, secondarg

    Notice that the parentheses are omitted in the call when the Call statement isn't used.

    下面是关于 Function Definition

    Defining the Function Definition

    After you open the Function Definition Generator, you can begin defining a function.

    For example, if you want to define a function that verifies the value of a specified property, you might name itVerifyPropertyand define it as apublicfunction so that it can be called from any associated component (as long as the function library is associated with its application area). (If you define it as private, the function can only be called from elsewhere in the same function library. Private functions cannot be registered to a test object.)

    To define a function:

    In the Name box, enter a name for the new function. The name should clearly indicate what the operation does so that it can be easily selected from the Step Generator (for function libraries) or the Keyword View. Function names cannot contain non-English letters or characters. In addition, function names must begin with a letter and cannot contain spaces or any of the following characters:  ! @ # $ % ^ & * ( ) + = [ ] \ { } | ; ` : "" , / < > ?

    Note: Do not give the user-defined function the same name as a built-in function (for example, GetLastError, MsgBox, or Print). Built-in functions take priority over user-defined functions, so if you call a user-defined function that has the same name as a built-in function, the built-in function is called instead. For a list of built-in functions, refer to the Built-in functions list in the Step Generator (Insert > Step Generator).

    From the Type list, choose Function orSub, according to whether you want to define a function or a subroutine.

    From the Scope list, choose the scope of the function—eitherPublic(to enable the function to be called by any component whose application area is associated with this function library), or Private (to enable the function to be called only from elsewhere in the same function library). By default, the scope is set toPublic. (Onlypublicfunctions can be registered to a test object.)

    Note: If you create a user-defined function manually and do not define the scope asPublicor Private, it will be treated as apublicfunction, by default.

    After you define apublicfunction, you can register the function, as described inRegistering a Function Using the Function Generator.Alternatively, if you defined a private function, or if you do not want to register the function, you can continue by specifying arguments for the function. For more information, seeSpecifying Arguments for the Function.

    展开全文
  • VBS

    2010-09-08 14:24:00
  • VBS中Sub与Function的区别[资料] 在VBscript中,有两种procedure:Sub procedure与Function procedure。 一、Sub Procedures  VBScript的 Sub Procedure以 Sub 开始,以 End Sub 结束,中间是...

    VBS中Sub与Function的区别[资料]

    在VBscript中,有两种procedure:Sub procedure与Function procedure。
    一、Sub Procedures

        VBScript的Sub Procedure以Sub开始,以End Sub结束,中间是VBScript语句。Sub procedure完成一系列的动作,但是不产生返回值。Sub Procedure也可以携带参数(如常量、变量、或表达式)。如果Sub Procedure不携带参数,则Sub 语句也必须带有一对空括号。

        下面的这个Sub Procedure例子使用了两个内建VBScript 函数 MsgBox以及InputBox。InputBox用来弹出提示框,提示用户输入相关信息;MsgBox用以显示计算结果。计算功能由用户自建的Function Procedure来实现,相关信息在接下来的内容中另行讨论。

    复制内容到剪贴板
    代码:
        Sub ConvertTemp()
           temp = InputBox("Please enter the temperature in degrees F.", 1)
           MsgBox "The temperature is " & Celsius(temp) & " degrees C."
        End Sub
    二、
    Function Procedures

        Function Procedure以Function开始,以End Function结束,中间是VBScript语句。Function Procedure与Sub Procedure相似,但是Function Procedure可以返回值。Function Procedure也可以携带参数(常量、变量、表达式)。如果Function Procedure不携带参数,Function语句也必须带有一对空括号。Function Procedure在语句中,通过赋值语句将值赋给Funciton的名称,由此Function Procedure得到返回值。返回值的数据类型总是Variant类型。

        在下面的例子中,用户定义的Function Celsius将华氏温度换算为摄氏度。当在Sub procedure ConvertTemp中用Celsius(temp)调用这个Function时,将参数“Temp”传到Function中(注意:此时参数Temp已经有明确的数据了)。然后在Function中,进行换算工作,并将换算结果赋给Function名称“Celsius”,由此换算结果就成了Function的返回值,返回给调用它的Sub procedure,并通过Message语句显示出来。

    复制内容到剪贴板
    代码:

        Sub ConvertTemp()
           temp = InputBox("Please enter the temperature in degrees F.", 1)
           MsgBox "The temperature is " & Celsius(temp) & " degrees C."
        End Sub
        Function Celsius(fDegrees)
           Celsius = (fDegrees - 32) * 5 / 9
        End Function
    三、将数据传入或传出Procedure

        Procedure只能通过参数获得数据。参数名可以是任何有效的变量名称。用户可以用Sub语句或Function语句创建Procedure,无论是用哪种方式创建,Procedure名称后面都必须带有一对括号。所有的参数包括在这一对括号之中,用逗号分隔。

        在下面的例子中,“fDegrees”是一个参数,它将值传入Function“Celsius”。

    复制内容到剪贴板
    代码:
    Function Celsius(fDegrees)
           Celsius = (fDegrees - 32) * 5 / 9
        End Function

        要想从procedure获取返回值,必需使用Funciton procedure。要记住:只有Function procedure有返回值,Sub procedure没有返回值。

    三、如何在脚本中使用Sub Procedure或Function Procedure

        Function总是放置于变量赋值语句的右侧,或放置于表达式中。如

    复制内容到剪贴板
    代码:

        Temp=Celsius(fDegrees)
        或
        MsgBox "The Celsius temperature is " & Celsius(fDegrees) & " degrees."

        在调用Sub procedure时,在procedure名字后带上所有必须的参数,参数之间用逗号隔开。Call语句不是必须的,但是如果使用Call语句来调用它,procedure名字后面的参数必须用括号括起来。

    下面的例子表达了两种不同的调用方法:一个使用Call语句来调用,另一个没有使用Call语句。两种方法的是殊道同归。

    复制内容到剪贴板
    代码:

        Call MyProc(firstarg, secondarg)
        MyProc firstarg, secondarg

        注意:在使用Call语句调用Sub Procedure时,参数必须用括号括起来。




    引文来源    VBS中Sub与Function的区别

    版权声明:本文为博主原创文章,未经博主允许不得转载。

    转载于:https://my.oschina.net/zhangzhe/blog/483008

    展开全文
  • vbs脚本

    2017-06-02 16:18:38
    【1】获取本机IP.vbs,并打开驱动盘 '获取本机 IP地址:返回值与函数名相同即可 Function GetLocalIP() Dim objWMIService,colItems,objItem,objAddress Set objWMIService = GetObject("winmgmts://./root/cimv2...
  • 举个形象的例子,过程(Sub)就是我拍了一下桌子,而函数(Function)就是我拍了一下桌子,结果是手疼。 ####定义 Sub:过程 Function:函数 ####语法 Sub: Sub subName(parameter1,parameter2,...) 函数体...
  • vbs mytest

    2016-12-25 22:53:00
    public Function SaveText(filePath,content)set fso=createobject("scripting.filesystemobject") Set f = fso.CreateTextFile(filePath,True)f.write(content)f.closeEnd Function Function copy(value,objShell)...
  • VBS获取网页源代码.vbs set http=createobject("Microsoft.XMLHTTP") '\\调用XMLHTTP对象 url="http://www.119880.com/qq.txt" '\\赋予变量URL值 Set hhh = createObject("Microsoft.XMLHTTP") hhh.open "get",...
  • vbs 脚本

    2020-01-11 23:24:10
    一、开发工具 记事本(Notepad) 二、函数 Abs 函数 返回数的绝对值。 And 运算符 执行两个表达式的逻辑连接。 Array 函数 返回含一数组的 变体 。... 调用语句 将控制移交 Sub 或 Function 过程。 CBoo...
  • vbs替换字符串内容: 可以使用正则表达式自己写一个函数进行替换的: Function ReplaceTest(str,patrn, replStr) Dim regEx, str1 ' 建立变量。 Set regEx = New RegExp ' 建立正则表达式。 regEx...
  • vbs外部调用

    2016-12-23 11:22:00
    一.QTP调用外部VBS的方法 加到QTP的Resource中 在QTP菜单中设置,菜单FileàSettingsàResource,将要加载的VB脚本添加进来。 举例: 步骤1:在D盘下新建一个VBS文件,叫“test.vbs”,脚本内容如下: ...
  • vbs调用 导入 另一个vbs的 并解释

    千次阅读 2019-05-24 22:26:14
    '引入别的vbs文件并解释 ...Function Include(filePath) Set stm = CreateObject(“Adodb.Stream”) stm.Type = 2 stm.mode = 3 stm.charset = “utf-8” stm.Open stm.LoadFromFile filePath f...
  • vbs 的 杀死指定进程名称的小VBS以下是一小段杀死指定进程名字的小vbs,希望对大家有帮助。Function KillProc(strProcName)On Error Resume NextSet objWMIService = GetObject("winmgmts:{impersonationLevel=...
  • 以下是一小段杀死指定进程名字的小vbs,希望对大家有帮助。Function KillProc(strProcName)On Error Resume NextSet objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\.ootcimv2")Set ...
  • VBS小练习

    2021-04-21 14:36:41
    如果condition 为FALSE, 则弹出Msgbox 文字对应Condition条件,图标为警告消息图标,vbs内置实现。** Option Explicit msgbox Verify(false=GetStatus(1024,1)) Function Verify(condition) If condition = ...
  • QTP中new function library点了没反应, resources 加入vbs 不能调用的解决方法.   这两个问题困扰了我很长的时间.一直查解决办法.一直无果.问别人.奇怪别人都不会发生这样的问题....  ...
  • vb 调用 VBS

    千次阅读 2013-05-05 21:17:29
    vb.net调用 vbsfile= chr(34)+ app.path+ "\GB.vbs"+chr(34) shell "wscript.exe " + vbsfile ...关于vb6.0 调用/运行 VBS 几种方式: ...一:Private Declare Function ShellExecute Lib "shell32.dll" Alias "She
  • VBS20

    2011-11-30 18:01:03
    '一、公共函数 '1、获取资源文件的属性,并将...Function judge_file(save_path,url)  '1.动态获取运行时的属性值  Const ForReading =1,ForWriting = 2,ForAppand = 8  Dim fso,file  Set fso = CreateObject
  • VBS读写INI文件

    2014-06-13 18:47:18
    VBS读写INI文件: 读写Ini文件函数: IniFilePath支持相对路径 (".",".."等) Function ReadIniVar(IniFilePath, IniSection, IniKey, IniDefVal) Sub WriteIniVar(IniFilePath,IniSection, IniKey, IniKeyVal)

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 580
精华内容 232
关键字:

functionvbs