<script language="VB" Runat="server">Dim result As Integer 'result 是全域變數
Function sum(a As Integer, b As Integer)
result = a + b 'result 是全域變數
End Function
result = a + b 'result 是全域變數
End Function
Function multiply(a As Integer, b As Integer)
Dim result As Integer 'result 是區域變數
result = a * b
End Function
Dim result As Integer 'result 是區域變數
result = a * b
End Function
Function factorial(c As Integer)
Dim i As Integer
result = 1
For i = 1 To c
Dim d As Integer = 2 'd 是區塊變數
result = i * d
Next
i = 1
result = 1
While i <= c
Dim e As Integer = 2 'e 是區塊變數
e *= i
i += 1
'result = d '錯誤, d 是不同程式區塊的變數, 無法存取
result = e '正確, e 是相同程式區塊的塊變數, 所以可以存取
End While
End Function
</script>
Dim i As Integer
result = 1
For i = 1 To c
Dim d As Integer = 2 'd 是區塊變數
result = i * d
Next
i = 1
result = 1
While i <= c
Dim e As Integer = 2 'e 是區塊變數
e *= i
i += 1
'result = d '錯誤, d 是不同程式區塊的變數, 無法存取
result = e '正確, e 是相同程式區塊的塊變數, 所以可以存取
End While
End Function
</script>
沒有留言:
張貼留言