| 1 |
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>References</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="Manual do Genius"><link rel="up" href="ch06.html" title="Chapter 6. Programming with GEL"><link rel="prev" href="ch06s07.html" title="Returning"><link rel="next" href="ch06s09.html" title="Lvalues"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">References</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06s07.html">Prev</a> </td><th width="60%" align="center">Chapter 6. Programming with GEL</th><td width="20%" align="right"> <a accesskey="n" href="ch06s09.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="genius-gel-references"></a>References</h2></div></div></div><p lang="en"> |
| 2 |
It may be necessary for some functions to return more than one value. |
| 3 |
This may be accomplished by returning a vector of values, but many |
| 4 |
times it is convenient to use passing a reference to a variable. |
| 5 |
You pass a reference to a variable to a function, and the function |
| 6 |
will set the variable for you using a dereference. You do not have |
| 7 |
to use references only for this purpose, but this is their main use. |
| 8 |
</p><p lang="en"> |
| 9 |
When using functions that return values through references |
| 10 |
in the argument list, just pass the variable name with an ampersand. |
| 11 |
For example the following code will compute an eigenvalue of a matrix |
| 12 |
<code class="varname">A</code> with initial eigenvector guess |
| 13 |
<code class="varname">x</code>, and store the computed eigenvector |
| 14 |
into the variable named <code class="varname">v</code>: |
| 15 |
</p><pre lang="en" class="programlisting">RayleighQuotientIteration (A,x,0.001,100,&v) |
| 16 |
</pre><p lang="en"> |
| 17 |
</p><p lang="en"> |
| 18 |
The details of how references work and the syntax is similar to the C language. |
| 19 |
The operator |
| 20 |
<code class="literal">&</code> references a variable |
| 21 |
and <code class="literal">*</code> dereferences a variable. Both can only be applied to an identifier, |
| 22 |
so <code class="literal">**a</code> is not a legal expression in GEL. |
| 23 |
</p><p lang="en"> |
| 24 |
References are best explained by an example: |
| 25 |
</p><pre lang="en" class="programlisting">a=1; |
| 26 |
b=&a; |
| 27 |
*b=2; |
| 28 |
</pre><p lang="en"> |
| 29 |
now <code class="varname">a</code> contains 2. You can also reference functions: |
| 30 |
</p><pre lang="en" class="programlisting">function f(x) = x+1; |
| 31 |
t=&f; |
| 32 |
*t(3) |
| 33 |
</pre><p lang="en"> |
| 34 |
gives us 4. |
| 35 |
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06s07.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch06.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch06s09.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Returning </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Lvalues</td></tr></table></div></body></html> |