局部变量在递归调用的 Lotusscript 函数中是否独立?
Will local variables be independent in recursively called Lotusscript function?
如果我有一个递归函数(Lotusscript)并且函数内部是一个局部声明的变量,调用的每次迭代都会将变量独立存储在内存中吗?
例如,我在循环 10 次的主函数上有一个计数器....它调用递归函数,在某些情况下,它会调用自身....但每次都传递不同的对象作为论据。递归函数在本地声明了自己的计数器变量。
假设调用了这个main函数,调用了一次递归函数,开始循环,自己数到十。在第 5 个循环中,它调用自身。由于设置了全局布尔值,此递归将结束,现在我有三个已知的局部变量,主函数和递归函数中的两个。
是否会独立跟踪这些计数器中的每一个,以便根据我在哪个函数中它知道它在它自己的十个循环中的位置?
希望我说清楚了。我正在尝试一个简单的概念验证功能,但它确实令人困惑。
谢谢
是的,它将是独立的:局部变量对于递归中的每个调用都是局部的,只要您不将它们用作参数,因为它们默认为 byref:
Sub RecurseMe( intParameter as Integer )
Dim intCount as Integer
Print "Called with:", intParameter
intParameter = intParameter + 1
intCount = intCount + 1
Print "IntCount: ", intCount
If intParameter < 3 then
Call RecurseMe( intParameter )
End If
Print "Exiting with: ", intParameter
End Sub
Dim intTest as Integer
intTest = 1
Call RecurseMe( intTest )
Print "Final result: ", intTest
将输出:
Called with: 1
IntCount: 1
Called with: 2
IntCount: 1
Exiting with: 3
Exiting with: 3
Finale result: 3
如您所见:intCount 总是在 sub 中重新初始化,intParameter 甚至会在调用 sub 中更改。
如果我有一个递归函数(Lotusscript)并且函数内部是一个局部声明的变量,调用的每次迭代都会将变量独立存储在内存中吗?
例如,我在循环 10 次的主函数上有一个计数器....它调用递归函数,在某些情况下,它会调用自身....但每次都传递不同的对象作为论据。递归函数在本地声明了自己的计数器变量。
假设调用了这个main函数,调用了一次递归函数,开始循环,自己数到十。在第 5 个循环中,它调用自身。由于设置了全局布尔值,此递归将结束,现在我有三个已知的局部变量,主函数和递归函数中的两个。
是否会独立跟踪这些计数器中的每一个,以便根据我在哪个函数中它知道它在它自己的十个循环中的位置?
希望我说清楚了。我正在尝试一个简单的概念验证功能,但它确实令人困惑。
谢谢
是的,它将是独立的:局部变量对于递归中的每个调用都是局部的,只要您不将它们用作参数,因为它们默认为 byref:
Sub RecurseMe( intParameter as Integer )
Dim intCount as Integer
Print "Called with:", intParameter
intParameter = intParameter + 1
intCount = intCount + 1
Print "IntCount: ", intCount
If intParameter < 3 then
Call RecurseMe( intParameter )
End If
Print "Exiting with: ", intParameter
End Sub
Dim intTest as Integer
intTest = 1
Call RecurseMe( intTest )
Print "Final result: ", intTest
将输出:
Called with: 1
IntCount: 1
Called with: 2
IntCount: 1
Exiting with: 3
Exiting with: 3
Finale result: 3
如您所见:intCount 总是在 sub 中重新初始化,intParameter 甚至会在调用 sub 中更改。