Octave – espressioni III – 35

eUd

Proseguo da qui, oggi qui.

Chiamata a funzioni
A function 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 sqrt computes the square root of a number.

A fixed set of functions are built-in, which means they are available in every Octave program. The sqrt function is one of these. In addition, you can define your own functions. See Functions and Scripts, for information about how to do this.

The way to use a function is with a function call expression, which consists of the function name followed by a list of arguments 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:

sqrt (x^2 + y^2)      # One argument
ones (n, m)           # Two arguments
rand ()               # No arguments

Each function expects a particular number of arguments. For example, the sqrt function must be called with a single argument, the number to take the square root of:

o221

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.

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 sqrt (argument) 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.

Unlike most languages, functions in Octave may return multiple values. For example, the following statement computes the singular value decomposition of the matrix a and assigns the three result matrices to u, s, and v.

o222

The left side of a multiple assignment expression is itself a list of expressions, and is allowed to be a list of variable names or index expressions. See also Index Expressions, and Assignment Ops.

Proseguo, sono qui.

Chiamata per valore
In Octave, unlike Fortran, [ma come nei linguaggi usuali,] function arguments are passed by value, which means that each argument in a function call is evaluated and assigned to a temporary location in memory before being passed to the function. There is currently no way to specify that a function parameter should be passed by reference instead of by value. This means that it is impossible to directly alter the value of a function parameter in the calling function. It can only change the local copy within the function body. For example, the function

function f (x, n)
  while (n-- > 0)
    disp (x);
  endwhile
endfunction

displays the value of the first argument n times. In this function, the variable n is used as a temporary variable without having to worry that its value might also change in the calling function. Call by value is also useful because it is always possible to pass constants for any function parameter without first having to determine that the function will not attempt to modify the parameter.

The caller may use a variable as the expression for the argument, but the called function does not know this: it only knows what value the argument had. For example, given a function called as

foo = "bar";
fcn (foo)

you should not think of the argument as being “the variable foo.” Instead, think of the argument as the string value, “bar“.

Even though Octave uses pass-by-value semantics for function arguments, values are not copied unnecessarily. For example,

x = rand (1000);
f (x);

does not actually force two 1000 by 1000 element matrices to exist unless the function f modifies the value of its argument. Then Octave must create a copy to avoid changing the value outside the scope of the function f, or attempting (and probably failing!) to modify the value of a constant or the value of a temporary result.

Avanti, sono qui.

Ricorsione
With some restrictions (Some of Octave’s functions are implemented in terms of functions that cannot be called recursively. For example, the ODE solver lsode is ultimately implemented in a Fortran subroutine that cannot be called recursively, so lsode should not be called either directly or indirectly from within the user-supplied function that lsode requires. Doing so will result in an error, recursive function calls are allowed. A recursive function is one which calls itself, either directly or indirectly. For example, here is an inefficient (It would be much better to use prod (1:n), or gamma (n+1) instead, after first checking to ensure that the value n is actually a positive integer.) way to compute the factorial of a given integer:

function retval = fact (n)
  if (n > 0)
    retval = n * fact (n-1);
  else
    retval = 1;
  endif
endfunction

Nota perso: funziona solo a volte, oltre a essere un pessimo modo di scrivere 😦

This function is recursive because it calls itself directly. It eventually terminates because each time it calls itself, it uses an argument that is one less than was used for the previous call. Once the argument is no longer greater than zero, it does not call itself, and the recursion ends.

The built-in variable max_recursion_depth specifies a limit to the recursion depth and prevents Octave from recursing infinitely.

Built-in Function: val = max_recursion_depth ()
Built-in Function: old_val = max_recursion_depth (new_val)
Built-in Function: max_recursion_depth (new_val, "local")

Query or set the internal limit on the number of times a function may be called recursively.
If the limit is exceeded, an error message is printed and control returns to the top level.
When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.

o223

:mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

Trackback

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: