<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Calling Functions (GNU Octave (version 5.1.0))</title> <meta name="description" content="Calling Functions (GNU Octave (version 5.1.0))"> <meta name="keywords" content="Calling Functions (GNU Octave (version 5.1.0))"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <link href="index.html#Top" rel="start" title="Top"> <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> <link href="Expressions.html#Expressions" rel="up" title="Expressions"> <link href="Call-by-Value.html#Call-by-Value" rel="next" title="Call by Value"> <link href="Advanced-Indexing.html#Advanced-Indexing" rel="prev" title="Advanced Indexing"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {margin-left: 3.2em} kbd {font-style: oblique} pre.display {font-family: inherit} pre.format {font-family: inherit} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} ul.no-bullet {list-style: none} --> </style> <link rel="stylesheet" type="text/css" href="octave.css"> </head> <body lang="en"> <a name="Calling-Functions"></a> <div class="header"> <p> Next: <a href="Arithmetic-Ops.html#Arithmetic-Ops" accesskey="n" rel="next">Arithmetic Ops</a>, Previous: <a href="Index-Expressions.html#Index-Expressions" accesskey="p" rel="prev">Index Expressions</a>, Up: <a href="Expressions.html#Expressions" accesskey="u" rel="up">Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Calling-Functions-1"></a> <h3 class="section">8.2 Calling Functions</h3> <p>A <em>function</em> is a name for a particular calculation. Because it has a name, you can ask for it by name at any point in the program. For example, the function <code>sqrt</code> computes the square root of a number. </p> <p>A fixed set of functions are <em>built-in</em>, which means they are available in every Octave program. The <code>sqrt</code> function is one of these. In addition, you can define your own functions. See <a href="Functions-and-Scripts.html#Functions-and-Scripts">Functions and Scripts</a>, for information about how to do this. </p> <a name="index-arguments-in-function-call"></a> <p>The way to use a function is with a <em>function call</em> expression, which consists of the function name followed by a list of <em>arguments</em> in parentheses. The arguments are expressions which give the raw materials for the calculation that the function will do. When there is more than one argument, they are separated by commas. If there are no arguments, you can omit the parentheses, but it is a good idea to include them anyway, to clearly indicate that a function call was intended. Here are some examples: </p> <div class="example"> <pre class="example">sqrt (x^2 + y^2) # <span class="roman">One argument</span> ones (n, m) # <span class="roman">Two arguments</span> rand () # <span class="roman">No arguments</span> </pre></div> <p>Each function expects a particular number of arguments. For example, the <code>sqrt</code> function must be called with a single argument, the number to take the square root of: </p> <div class="example"> <pre class="example">sqrt (<var>argument</var>) </pre></div> <p>Some of the built-in functions take a variable number of arguments, depending on the particular usage, and their behavior is different depending on the number of arguments supplied. </p> <p>Like every other expression, the function call has a value, which is computed by the function based on the arguments you give it. In this example, the value of <code>sqrt (<var>argument</var>)</code> is the square root of the argument. A function can also have side effects, such as assigning the values of certain variables or doing input or output operations. </p> <p>Unlike most languages, functions in Octave may return multiple values. For example, the following statement </p> <div class="example"> <pre class="example">[u, s, v] = svd (a) </pre></div> <p>computes the singular value decomposition of the matrix <code>a</code> and assigns the three result matrices to <code>u</code>, <code>s</code>, and <code>v</code>. </p> <p>The left side of a multiple assignment expression is itself a list of expressions, that is, a list of variable names potentially qualified by index expressions. See also <a href="Index-Expressions.html#Index-Expressions">Index Expressions</a>, and <a href="Assignment-Ops.html#Assignment-Ops">Assignment Ops</a>. </p> <table class="menu" border="0" cellspacing="0"> <tr><td align="left" valign="top">• <a href="Call-by-Value.html#Call-by-Value" accesskey="1">Call by Value</a>:</td><td> </td><td align="left" valign="top"> </td></tr> <tr><td align="left" valign="top">• <a href="Recursion.html#Recursion" accesskey="2">Recursion</a>:</td><td> </td><td align="left" valign="top"> </td></tr> <tr><td align="left" valign="top">• <a href="Access-via-Handle.html#Access-via-Handle" accesskey="3">Access via Handle</a>:</td><td> </td><td align="left" valign="top"> </td></tr> </table> <hr> <div class="header"> <p> Next: <a href="Arithmetic-Ops.html#Arithmetic-Ops" accesskey="n" rel="next">Arithmetic Ops</a>, Previous: <a href="Index-Expressions.html#Index-Expressions" accesskey="p" rel="prev">Index Expressions</a>, Up: <a href="Expressions.html#Expressions" accesskey="u" rel="up">Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> </body> </html>