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
|
<html lang="en">
<head>
<title>Returning from a Function - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Functions-and-Scripts.html#Functions-and-Scripts" title="Functions and Scripts">
<link rel="prev" href="Variable_002dlength-Return-Lists.html#Variable_002dlength-Return-Lists" title="Variable-length Return Lists">
<link rel="next" href="Default-Arguments.html#Default-Arguments" title="Default Arguments">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<a name="Returning-from-a-Function"></a>
<p>
Next: <a rel="next" accesskey="n" href="Default-Arguments.html#Default-Arguments">Default Arguments</a>,
Previous: <a rel="previous" accesskey="p" href="Variable_002dlength-Return-Lists.html#Variable_002dlength-Return-Lists">Variable-length Return Lists</a>,
Up: <a rel="up" accesskey="u" href="Functions-and-Scripts.html#Functions-and-Scripts">Functions and Scripts</a>
<hr>
</div>
<h3 class="section">11.6 Returning from a Function</h3>
<p>The body of a user-defined function can contain a <code>return</code> statement.
This statement returns control to the rest of the Octave program. It
looks like this:
<pre class="example"> return
</pre>
<p>Unlike the <code>return</code> statement in C, Octave's <code>return</code>
statement cannot be used to return a value from a function. Instead,
you must assign values to the list of return variables that are part of
the <code>function</code> statement. The <code>return</code> statement simply makes
it easier to exit a function from a deeply nested loop or conditional
statement.
<p>Here is an example of a function that checks to see if any elements of a
vector are nonzero.
<pre class="example"> function retval = any_nonzero (v)
retval = 0;
for i = 1:length (v)
if (v (i) != 0)
retval = 1;
return;
endif
endfor
printf ("no nonzero elements found\n");
endfunction
</pre>
<p>Note that this function could not have been written using the
<code>break</code> statement to exit the loop once a nonzero value is found
without adding extra logic to avoid printing the message if the vector
does contain a nonzero element.
<div class="defun">
— Keyword: <b>return</b><var><a name="index-return-751"></a></var><br>
<blockquote><p>When Octave encounters the keyword <code>return</code> inside a function or
script, it returns control to the caller immediately. At the top level,
the return statement is ignored. A <code>return</code> statement is assumed
at the end of every function definition.
</p></blockquote></div>
</body></html>
|