VBScript - 程序

什么是函数?

函数是一组可重用的代码,可以在程序的任何地方调用.这消除了一遍又一遍地编写相同代码的需要.这将使程序员能够将大型程序划分为许多小型且易于管理的功能.除了内置函数,VBScript还允许我们编写用户定义的函数.本节将向您解释如何在VBScript中编写自己的函数.

函数定义

在使用函数之前,我们需要定义该特定函数功能.在VBScript中定义函数的最常用方法是使用Function关键字,后跟唯一的函数名称,它可能带有也可能不带有参数列表和带有 End Function 关键字的语句,表示函数的结束.

基本语法如下所示 :

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Function Functionname(parameter-list)
            statement 1
            statement 2
            statement 3
            .......
            statement n
         End Function

      </script>
   </body>
</html>

示例

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Function sayHello()
            msgbox("Hello there")
         End Function

      </script>
   </body>
</html>

调用函数

要在脚本稍后的某个地方调用函数,您只需要编写名称该函数与调用关键字.

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Function sayHello()
            msgbox("Hello there")
         End Function

         Call sayHello()
        
      </script>
   </body>
</html>

函数参数

到目前为止,我们已经看到没有参数的函数,但是有一个设施可以传递不同的调用函数时的参数.可以在函数内捕获这些传递的参数,并且可以对这些参数进行任何操作.使用调用关键字调用函数.

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Function sayHello(name, age)
            msgbox( name & " is " & age & " years old.")
         End Function

         Call sayHello("Tutorials point", 7)

      </script>
   </body>
</html>

从函数返回值

VBScript函数可以有一个可选的return语句.如果要从函数返回值,则必须执行此操作.例如,你可以在一个函数中传递两个数字,然后你可以期望函数在你的调用程序中返回它们的乘法.

注意 : 函数可以返回由逗号分隔的多个值作为分配给函数名称本身的数组.

示例

此函数接受两个参数并连接它们并返回调用程序中的结果.在VBScript中,使用函数名称从函数返回值.如果要返回两个或更多值,则返回带有值数组的函数名称.在调用程序中,结果存储在结果变量中.

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Function concatenate(first, last)
            Dim full
            full = first & last
            concatenate = full  'Returning the result to the function name itself
         End Function

      </script>
   </body>
</html>

现在,我们可以将此函数调用如下 :

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Function concatenate(first, last)
            Dim full
            full = first & last
            concatenate = full  'Returning the result to the function name itself
         End Function
         ' Here is the usage of returning value from  function. 
         dim result
            result = concatenate("Zara", "Ali")
        msgbox(result)
      </script>
   </body>
</html>

子程序

子程序与功能相似,但差异很小.

  • 子程序DONOT返回值,而函数可能会或可能不会返回值.

  • 子程序可以不带调用关键字调用.

  • 子程序总是包含在 Sub 结束子语句.

示例

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Sub sayHello()
            msgbox("Hello there")
         End Sub

      </script>
   </body>
</html>

调用程序

要在脚本稍后的某处调用过程,您只需要写入名称该程序有或没有调用关键字.

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Sub sayHello()
            msgbox("Hello there")
         End Sub
         sayHello()

      </script>
   </body>
</html>

函数的高级概念

有很多关于VBScript函数的知识.我们可以通过value或byreference传递参数.请点击其中的每一个了解更多信息.

  • ByVal-按值传递参数

  • ByRef-通过参考传递参数