File: flow_return.html

package info (click to toggle)
freemat 4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 174,756 kB
  • ctags: 67,023
  • sloc: cpp: 351,059; ansic: 255,892; sh: 40,590; makefile: 4,387; perl: 4,058; asm: 3,313; pascal: 2,718; fortran: 1,722; ada: 1,681; ml: 1,360; cs: 879; csh: 795; python: 430; sed: 162; lisp: 160; awk: 5
file content (105 lines) | stat: -rw-r--r-- 2,100 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<HTML>
<HEAD>
<TITLE>RETURN Return From Function
</TITLE>
</HEAD>
<BODY>
<H2>RETURN Return From Function
</H2>
<P>
Section: <A HREF=sec_flow.html> Flow Control </A>
<H3>Usage</H3>
The <code>return</code> statement is used to immediately return from
a function, or to return from a <code>keyboard</code> session.  The 
syntax for its use is
<PRE>
  return
</PRE>
<P>
Inside a function, a <code>return</code> statement causes FreeMat
to exit the function immediately.  When a <code>keyboard</code> session
is active, the <code>return</code> statement causes execution to
resume where the <code>keyboard</code> session started.
<H3>Example</H3>
In the first example, we define a function that uses a
<code>return</code> to exit the function if a certain test condition 
is satisfied.
<P>
<PRE>
     return_func.m
function ret = return_func(a,b)
  ret = 'a is greater';
  if (a &gt; b)
    return;
  end
  ret = 'b is greater';
  printf('finishing up...\n');

</PRE>
<P>
Next we exercise the function with a few simple test
cases:
<PRE>
--&gt; return_func(1,3)
finishing up...

ans = 
b is greater
--&gt; return_func(5,2)

ans = 
a is greater
</PRE>
<P>
In the second example, we take the function and rewrite
it to use a <code>keyboard</code> statement inside the <code>if</code> statement.
<P>
<PRE>
     return_func2.m
function ret = return_func2(a,b)
  if (a &gt; b)
     ret = 'a is greater';
     keyboard;
  else
     ret = 'b is greater';
  end
  printf('finishing up...\n');

</PRE>
<P>
Now, we call the function with a larger first argument, which
triggers the <code>keyboard</code> session.  After verifying a few
values inside the <code>keyboard</code> session, we issue a <code>return</code>
statement to resume execution.
<PRE>
--&gt; return_func2(2,4)
finishing up...

ans = 
b is greater
--&gt; return_func2(5,1)
[return_func2,4]--&gt; ret

ans = 
a is greater
[return_func2,4]--&gt; a

ans = 
 5 

[return_func2,4]--&gt; b

ans = 
 1 

[return_func2,4]--&gt; return
finishing up...

ans = 
a is greater
</PRE>
<P>
</BODY>
</HTML>