Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 3e60ff9d4d6f58c8fbd17208f14089fa > files > 354

octave-doc-3.2.3-3mdv2010.0.i586.rpm

<html lang="en">
<head>
<title>Returning From a Function - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<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:&nbsp;<a rel="next" accesskey="n" href="Default-Arguments.html#Default-Arguments">Default Arguments</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Variable_002dlength-Return-Lists.html#Variable_002dlength-Return-Lists">Variable-length Return Lists</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Functions-and-Scripts.html#Functions-and-Scripts">Functions and Scripts</a>
<hr>
</div>

<h3 class="section">11.5 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">
&mdash; Keyword: <b>return</b><var><a name="index-return-610"></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>