0

What is the difference between a function handle and a function itself in MATLAB? Is it like the difference between a function pointer and function in C/C++?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Kicr
  • 145
  • 1
  • 12
  • Related/Possible duplicate: [What is a function handle and how is it useful?](https://stackoverflow.com/q/796935/52738) – gnovice Mar 05 '18 at 14:33

1 Answers1

0

I have no idea about C/C++ but in MATLAB a function is a non-anonymous program which (possibly) takes input and (possibly) gives output, and is defined using the function keyword, e.g.

function a = MySum(b)
    a = sum(b);
end

whereas a function handle is a sort of short-cut to a function which is anonymous, i.e. can't be called from outside that script. It is created using the @ syntax:

f=@(b)sum(b);

The function is saved as a .m file, with the function name as file name, so, using the above example, MySum.m. Having saved this on your path, and defined the anonymous function as per the above this is how you call them:

b = [1 2];
f=@(b)sum(b);
aFunction = MySum(b); % = 3
aAnon = f(b); % =3

If we now have a new script on the same path, we can't use the anonymous function if we don't define it, try:

b = [1 2];
aFunction = MySum(b); % = 3
aAnon = f(b); % gives error that f is undefined

If you create a script outside your path, also the function itself will fail:

b = [1 2];
aFunction = MySum(b); % gives error that MySum is undefined
aAnon = f(b); % gives error that f is undefined

Function handles are used for several reasons, of which the two most prominent in my opinion: inside "disguised loop functions" such as arrayfun or bsxfun which require a syntax like arrayfun(@function,array), or when using e.g. a certain combination of functions very often in a script, e.g. nnz(rand(size(N)>M), to shorten the sequence: f = @(M,N)nnz(rand(size(N)>M) and now one can call f(N,M) instead of nnz(rand(size(N)>M).

Following Wolfie's comment there is indeed a dual nature of a function handle: it can act as a pointer, i.e. how f in the above example is a pointer to the function sum, but can also be a function in it's own right, like the combination of functions nnz(rand(size(N)>M) or things like a polynomial: f=@(b) b.^2 + 5*b + 1.


Note on the "possibly" takes in-/output:

You could define a function as

function MySine()
    x = 1:10;
    y = sin(x);
    plot(x,y)
end

which is a perfectly valid function which takes no in- or output arguments.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 2
    Reader's note: a function handle can *act* like a pointer, in that your example `f=@(b)sum(b)` is a pointer to the function `sum`. Might be worth mentioning though that anonymous functions can be functions in their own right, like `f=@(b) b.^2 + 5*b + 1` – Wolfie Mar 05 '18 at 14:52
  • How about create function handle based on the existing ones? Like, what is the difference between `f=@sum` and `sum`? – Kicr Mar 06 '18 at 05:58
  • @Rickyim readability. It's perfectly possible (as I've shown with multiple functions in my example already), just difficult to read. It's also necessary in things like `arrayfun(@sum,array)`, also, as previously mentioned. – Adriaan Mar 06 '18 at 07:05
  • Thanks! Is the latter reason that it is not possible to pass the function name itself to a function requiring function handle? – Kicr Mar 06 '18 at 07:44
  • @Rickyim yes. You don't want to call the function itself within a call to `arrayfun`, but rather tell `arrayfun` that under the hood it needs to use that specific function. So in a sense that's a C/C++ like pointer I guess. `arrayfun(@sum,sum(array))` would work as well, then you use both the *function* sum, as well as the *handle* to it (basically you create a new array using `sum(array)` and let `arrayfun` operate on that using `sum`.) I suggest you just play around a bit with function handles and anonymous functions to get a feel for them. – Adriaan Mar 06 '18 at 07:56