A
Alok
I have some confusion about recursive function calls and definition of
variables in those functions. Consider these two code snippets for the
variable "b".
<PRE>
# def_stack.rb
def call1()
print "call1()\n"
b = "something"
if defined? b
print "defined? b is defined " + (defined? b).to_s + " with value " +
b.to_s + "\n"
else
print "defined? b is not defined " + (defined? b).to_s + " with value
" + b.to_s + "\n"
end
call2()
end
def call2()
print "\n\ncall2()\n"
if defined? b
print "defined? b is defined " + (defined? b).to_s + " with value " +
b.to_s + "\n"
else
print "defined? b is not defined " + (defined? b).to_s + ".\n"
end
end
call1()
</PRE>
outputs >>>
call1()
defined? b is defined local-variable with value something
call2()
defined? b is not defined .
Whereas for a recursive function
<PRE>
# def_recurse.rb
def call1( a )
print "\n\ncall1( " + a.to_s + " )\n"
if a == 1
b = "something"
recurse = true
else
recurse = false
end
if defined? b
print "defined? b is defined " + (defined? b).to_s + " with value " +
b.to_s + "\n"
else
print "defined? b is not defined " + (defined? b).to_s + ".\n"
end
call1( a + 1 ) if recurse
end
call1( 1 )
</PRE>
outputs >>>
call1( 1 )
defined? b is defined local-variable with value something
call1( 2 )
defined? b is defined local-variable with value
Why is the difference a difference for the definition of variable b in
the two functions. In particular, why is it that for a recursive
function the variable b appears defined in second call even though it
was never defined there.
I guess that a symbol table check routine looks for a variable
definition using the variable name & function name alone (for this
e.g.) and not using variable name & function name & function instance.
If this is the case, then it would explain why the the variable appears
undefined for the function call stack example.
Would much appreciate responses which can clarify what the ruby
interpreter does here.
variables in those functions. Consider these two code snippets for the
variable "b".
<PRE>
# def_stack.rb
def call1()
print "call1()\n"
b = "something"
if defined? b
print "defined? b is defined " + (defined? b).to_s + " with value " +
b.to_s + "\n"
else
print "defined? b is not defined " + (defined? b).to_s + " with value
" + b.to_s + "\n"
end
call2()
end
def call2()
print "\n\ncall2()\n"
if defined? b
print "defined? b is defined " + (defined? b).to_s + " with value " +
b.to_s + "\n"
else
print "defined? b is not defined " + (defined? b).to_s + ".\n"
end
end
call1()
</PRE>
outputs >>>
call1()
defined? b is defined local-variable with value something
call2()
defined? b is not defined .
Whereas for a recursive function
<PRE>
# def_recurse.rb
def call1( a )
print "\n\ncall1( " + a.to_s + " )\n"
if a == 1
b = "something"
recurse = true
else
recurse = false
end
if defined? b
print "defined? b is defined " + (defined? b).to_s + " with value " +
b.to_s + "\n"
else
print "defined? b is not defined " + (defined? b).to_s + ".\n"
end
call1( a + 1 ) if recurse
end
call1( 1 )
</PRE>
outputs >>>
call1( 1 )
defined? b is defined local-variable with value something
call1( 2 )
defined? b is defined local-variable with value
Why is the difference a difference for the definition of variable b in
the two functions. In particular, why is it that for a recursive
function the variable b appears defined in second call even though it
was never defined there.
I guess that a symbol table check routine looks for a variable
definition using the variable name & function name alone (for this
e.g.) and not using variable name & function name & function instance.
If this is the case, then it would explain why the the variable appears
undefined for the function call stack example.
Would much appreciate responses which can clarify what the ruby
interpreter does here.