2.39. Procedure return
A procedure can be terminated by a goto to a label
at the end of the procedure:
Start felix section to tut/tutorial/tut-1.39-0.flx[1
/1
]
1: #line 4016 "./lpsrc/flx_tutorial.pak"
2:
3:
4:
5:
6: proc f(x:int)
7: {
8: if x == 0 goto zero;
9: print x; endl;
10: goto finished;
11: zero:>
12: print "Zero"; endl;
13: finished:>
14: }
15:
16: f(1);
17: f(0);
Start data section to tut/tutorial/tut-1.39-0.expect[1
/1
]
A slightly more structured way of doing this
involves the procedural return statement:
Start felix section to tut/tutorial/tut-1.39-1.flx[1
/1
]
1: #line 4043 "./lpsrc/flx_tutorial.pak"
2:
3:
4:
5: proc f(x:int)
6: {
7: if x == 0 goto zero;
8: print x; endl;
9: return;
10: zero:>
11: print "Zero"; endl;
12: }
13:
14: f(1);
15: f(0);
Start data section to tut/tutorial/tut-1.39-1.expect[1
/1
]
This can also be shortened by using a the jump statement:
Start felix section to tut/tutorial/tut-1.39-2.flx[1
/1
]
1: #line 4067 "./lpsrc/flx_tutorial.pak"
2:
3:
4:
5: proc f(x:int)
6: {
7: if x == 0 goto zero;
8: print x; jump endl;
9: zero:>
10: print "Zero"; jump endl;
11: }
12:
13: f(1);
14: f(0);
Start data section to tut/tutorial/tut-1.39-2.expect[1
/1
]
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.