A function is a value, which can be assigned to a variable, stored in a list, passed to a function, or created and returned by another function. A function does not need a "handle" -- it can handle itself just fine.
function n -> s s = 0; for k = 1 : n s += 1 / k^2; end endNote that the function keyword must be matched by an end. Functions are "nameless". To be able to call a function, it should be assigned to a variable. For example
h = function n -> s s = 0; for k = 1 : n s += 1 / k^2; end endNow to call the function with argument value n=1000, one can do h(10000).
If a function definition just contains a single statement, then the
function and end
keyword are not necessary. For example, f = (x, y) -> (x+y) / (x^
2+y^ 2+1) defines the function
.
A function definition is no more special than assigning a constant value to a variable. Therefore, a program can contain as many function definitions as needed; function definitions can contain function definitions and can return function definitions, etc.
There is no need for feval. When you pass a function value to another function, just pass it, there is no need to play with strings, ``handle''s or feval.
Polynomial is implemented as a built-in function poly. It has a public parameter coeff. No polyeval is necessary as a polynomial is already a function and can be called directly. For example
>> p = poly; // make a copy of the built-in function poly, now p(x) = 1; >> p.coeff = [3, -2, 1]; // update the coefficient, now p(x) = 3 - 2x + x^2 >> p(10) // evaluate the polynomial 83 >> int(p, [0, 2]) // integrate p(x) from 0 to 2 4.666666667Or a new polynomial can be spawned by provided the values of the parameters
>> p = poly[[3, -2, 1]]; // spawn a new polymial by given the value of the // only public parameter >> p(10) // evaluate the polynomial 83 >> int(p, [0, 2]) // integrate p(x) from 0 to 2 4.666666667
E.g., sin - cos defines a function
.
There are no "nargin" and "nargout" inside a function body to tell the number of input and output arguments.
The default argument value is specified in the function definition header. For example
f = (x, alpha = 1) -> sqrt(x^2 + alpha^2);Then f(x) is equivalent to f(x, 1) since the argument alpha receives the default value.
If no default value is specified, the argument will receive null when no value is passed to it. For example, here f() is equivalent to f([], 1), as both arguments now receive default values.
The symbol * is equivalent to default value. So in function call f(*, 2), the first argument receives the default value and the second receives 2.
If a function expects one argument value but receives more than one, then those values are made a list and passed to the function as a whole. For example
f = function args -> v ... endThen when f is called by f(x, y, z, w), args receives the list (x,y,z,w).
On the other hand, if f expects a bunch of argument values, but passed one list, then the entries of the list will be retrieved and passed to those arguments. For example
f = function (x, y, z, w) -> v ... endThen
args = (a, b, c, d); f(args)is equivalent to f(a,b,c,d).
A function that has multiple return values always returns a list of those return values, regardless of how many values the function call is assigned to.
For example,
f = function (x, y) -> (z, w) // function definition --- should assign values to z and w endThen (u, v) = f(x, y) assigns the two return values to local variables u and v, while v = f(x,y) returns a list of two values. Note that in (u, v) = f(x, y) and v = f(x,y), the behavior of v = f(x,y) is not affected by the assignment.
Calling f with no argument is done by f(). The empty pair of
round brackets is necessary. Without it, the value of the expression
f is the function itself, not a call to the function without argument.
There is only ONE global variable, whose name is global. global itself cannot be assigned to. global is a structure, which may have a bunch of fields. You can modify a field, and can add new fields to it also. For example, you can add a new field display_format to global
global.display_format = "short";Now global.display_format can be accessed anywhere. Without the keyword global, display_format would still refer to the same thing, unless a local variable display_format has been defined.
Function definitions can carry parameters so that they can be customized later. For example,
function
can be defined as
f = function [alpha = 1] x -> y y = sqrt(x^2+alpha^2); endwhere the default value of the parameter is
f.alpha = 3;or make a anoter copy of f with
g = f; f.alpha = 3;or
g = f[3];
Functions can be called with only part of the argument values given . The outcome of such a function call
is a new function. For example, if is defined by
f = function (t, a, b, c, d) -> z .... endThen
g = f(., ., 5, -2, 3);where b, c, d are given values 5, -2, and 3 respectively.
New function g behaves completely like a user defined function, and user doesn't need to know that it is not defined directly in a file. If you want to integrate f as a function of t at given values of parameters a, b, c, d, you can do it like
int(f(., 1, 5, -2, 3), [0, 1]);The effect is the
oz 2009-12-22