1.37. Procedure return
A procedure can be terminated by a goto to a label
at the end of the procedure:
Start C++ section to tut/examples/tut146.flx[1
/1
]
1: include "std";
2:
3: proc f(x:int)
4: {
5: if x == 0 goto zero;
6: print x; endl;
7: goto finished;
8: zero:>
9: print "Zero"; endl;
10: finished:>
11: }
12:
13: f(1);
14: f(0);
A slightly more structured way of doing this
involves the procedural return statement:
Start C++ section to tut/examples/tut147.flx[1
/1
]
1: include "std";
2:
3: proc f(x:int)
4: {
5: if x == 0 goto zero;
6: print x; endl;
7: return;
8: zero:>
9: print "Zero"; endl;
10: }
11:
12: f(1);
13: f(0);
14:
This can also be shortened by using a the jump statement:
Start C++ section to tut/examples/tut148.flx[1
/1
]
1: include "std";
2:
3: proc f(x:int)
4: {
5: if x == 0 goto zero;
6: print x; jump endl;
7: zero:>
8: print "Zero"; jump endl;
9: }
10:
11: f(1);
12: f(0);
13:
which is equivalent to a call statement followed by a
return, which in turn is equivalent to a call
followed by a goto the end of the procedure.