- VBScript 教程
- VBScript - 首页
- VBScript - 概述
- VBScript - 语法
- VBScript - 启用
- VBScript - 位置
- VBScript - 变量
- VBScript - 常量
- VBScript - 运算符
- VBScript - 决策
- VBScript - 循环
- VBScript - 事件
- VBScript - Cookies
- VBScript - 数字
- VBScript - 字符串
- VBScript - 数组
- VBScript - 日期
- VBScript 高级
- VBScript - 过程
- VBScript - 对话框
- VBScript - 面向对象
- VBScript - 正则表达式
- VBScript - 错误处理
- VBScript - 杂项语句
- VBScript 有用资源
- VBScript - 问答
- VBScript - 快速指南
- VBScript - 有用资源
- VBScript - 讨论
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>
函数参数
到目前为止,我们已经看到了没有参数的函数,但有一个功能是在调用函数时传递不同的参数。这些传递的参数可以在函数内部捕获,并且可以在这些参数上进行任何操作。函数使用 Call 关键字进行调用。
<!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>
子过程
子过程类似于函数,但有一些区别。
子过程不返回值,而函数可能返回或不返回值。
子过程可以不带 call 关键字调用。
子过程始终包含在 Sub 和 End 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 函数还有很多需要学习的地方。我们可以按值或按引用传递参数。请点击每个选项以了解更多信息。