tags:

views:

41

answers:

2

How can I return from a proc to a higher context?
For example: If proc X called another proc Y which called a third proc Z - is there a way to return from Z directly back to X ?

A: 

if You are call "x process" from "z proc" then loop will create in the your process flow..

heyram
I actually I meant to return to the place where Y was called in X, to continue the program from there. (Y will not be called over and over again)
thedp
i think below answer is suitable for u
heyram
@heyram: you might want to consider rewriting your answer to use proper words. Using phrases like "u r" for "you are" lower the credibility of your answer by a few orders of magnitude.
Bryan Oakley
+6  A: 

From 8.5 onwards, yes. The return command has a -level option which is used to do just that:

return -level 2 $someValue

Thus, for example:

proc X {} {
    puts "X - in"
    Y
    puts "X - out"
}
proc Y {} {
    puts "Y - in"
    Z
    puts "Y - out"
}
proc Z {} {
    puts "Z - in"
    return -level 2 "some value"
    puts "Z - out"
}
X

produces this output:

X - in
Y - in
Z - in
X - out

Note that doing this reduces the reusability of Z, but that's your business.

Donal Fellows
I've always used `uplevel 1 [list return $some_value]` to do this which works even on older versions of tcl.
slebetman
@slebetman: Did you ever check that in a simple harness like the one above? Because when I do, it also prints out “`Y - out`”, indicating that it isn't doing what you're hoping it does…
Donal Fellows