
- 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 函式還有很多需要學習的地方。我們可以按值或按引用傳遞引數。請點選每個選項以瞭解更多資訊。