File: Default-Arguments.html

package info (click to toggle)
octave3.2 3.2.4-8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 62,936 kB
  • ctags: 37,353
  • sloc: cpp: 219,497; fortran: 116,336; ansic: 10,264; sh: 5,508; makefile: 4,245; lex: 3,573; yacc: 3,062; objc: 2,042; lisp: 1,692; awk: 860; perl: 844
file content (71 lines) | stat: -rw-r--r-- 3,216 bytes parent folder | download
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
<html lang="en">
<head>
<title>Default Arguments - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<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="Returning-From-a-Function.html#Returning-From-a-Function" title="Returning From a Function">
<link rel="next" href="Function-Files.html#Function-Files" title="Function Files">
<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">
<p>
<a name="Default-Arguments"></a>
Next:&nbsp;<a rel="next" accesskey="n" href="Function-Files.html#Function-Files">Function Files</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Returning-From-a-Function.html#Returning-From-a-Function">Returning From a Function</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.6 Default Arguments</h3>

<p><a name="index-default-arguments-611"></a>
Since Octave supports variable number of input arguments, it is very useful
to assign default values to some input arguments.  When an input argument
is declared in the argument list it is possible to assign a default
value to the argument like this

<pre class="example">     function <var>name</var> (<var>arg1</var> = <var>val1</var>, ...)
       <var>body</var>
     endfunction
</pre>
   <p class="noindent">If no value is assigned to <var>arg1</var> by the user, it will have the
value <var>val1</var>.

   <p>As an example, the following function implements a variant of the classic
&ldquo;Hello, World&rdquo; program.
<pre class="example">     function hello (who = "World")
       printf ("Hello, %s!\n", who);
     endfunction
</pre>
   <p class="noindent">When called without an input argument the function prints the following
<pre class="example">     hello ();
          -| Hello, World!
</pre>
   <p class="noindent">and when it's called with an input argument it prints the following
<pre class="example">     hello ("Beautiful World of Free Software");
          -| Hello, Beautiful World of Free Software!
</pre>
   <p>Sometimes it is useful to explicitly tell Octave to use the default value
of an input argument.  This can be done writing a &lsquo;<samp><span class="samp">:</span></samp>&rsquo; as the value
of the input argument when calling the function.
<pre class="example">     hello (:);
          -| Hello, World!
</pre>
   </body></html>