Μπορεί να είναι απαραίτητο για μερικές συναρτήσεις να επιστραφούν περισσότερες από μία τιμές. Αυτό μπορεί να επιτευχθεί επιστρέφοντας ένα διάνυσμα τιμών, αλλά πολλές φορές, είναι βολικό να χρησιμοποιήσετε το πέρασμα μιας αναφοράς σε μια μεταβλητή. Περνάτε μια αναφορά σε μια μεταβλητή σε μια συνάρτηση και η συνάρτηση θα ορίσει τη μεταβλητή για σας χρησιμοποιώντας μια αποαναφορά. Δεν πρέπει να χρησιμοποιείτε αναφορές μόνο για αυτόν το σκοπό, αλλά αυτή είναι η κύρια χρήση τους.
When using functions that return values through references
in the argument list, just pass the variable name with an ampersand.
For example the following code will compute an eigenvalue of a matrix
A with initial eigenvector guess
x, and store the computed eigenvector
into the variable named v:
RayleighQuotientIteration (A,x,0.001,100,&v)
Οι λεπτομέρειες του πώς δουλεύουν οι αναφορές και η σύνταξη είναι παρόμοιες με τη γλώσσα C. Ο τελεστής & αναφέρεται σε μια μεταβλητή και το * αποαναφέρει μια μεταβλητή. Αμφότεροι μπορούν να εφαρμοστούν μόνο σε ένα αναγνωριστικό, έτσι το **a δεν είναι επιτρεπτή παράσταση στο GEL.
References are best explained by an example:
a=1;
b=&a;
*b=2;
now a contains 2. You can also reference functions:
function f(x) = x+1;
t=&f;
*t(3)
gives us 4.
The first time a variable is referenced, it is created in the current
context unless it exists. So if we make a function that sets the variable to 2,
such as
function f(k) = (*k=2), then the
call
f(&n) will declare a new variable n
then the function
f will run and set it to 2. So there is
no need to write something like
n=0;f(&n). A new variable in the current
context is created even if a
variable exists in a higher context, such as in the global context.
So for example after
n=3;
function f(k) = (*k=2);
function g() = (f(&n);print(n));
g();
the variable n is still 3 and not 2. That is because inside
g, when n is referenced, it is a new
variable in the context of g. The function
f then sets this n to 2,
and then 2 is printed, but the n in the global context
is not touched. So we can use references to get values out of functions and
we do not need to explicitly declare them, nor do we have to worry about messing up
global variables that the user might be using for something else.