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>

调用函数

要在脚本后面的某个地方调用一个函数,您只需要简单地用Call关键字编写该函数的名称。

<!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中,使用函数名称从函数返回值。如果要返回两个或多个值,则函数名称将返回一个值数组。在调用程序中,结果存储在result变量中。

<!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返回值,而函数可能会也可能不会返回值。
  • 子过程无需调用关键字即可调用。
  • 子过程始终包含在SubEnd Sub语句内。

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

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

呼叫程序

要在脚本后面的某个地方调用一个过程,您只需要编写该过程的名称即可,无论是否带有Call关键字。

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

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

功能的高级概念

关于VBScript函数,有很多东西要学习。我们可以传递参数byvalue或byreference。请单击其中每个以了解更多信息。

作者:terry,如若转载,请注明出处:https://www.web176.com/vbscript/1105.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2020年12月4日 下午7:14
下一篇 2020年12月7日 上午11:27

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注