Accelerating the pace of engineering and science

# Documentation

## Define and Pass Function Handles for Code Acceleration

The following code example shows how to define and call function handles for code acceleration.

```function [y1, y2] = addval(m)
%#codegen

disp(m);

% Pass function handle to addone
%  to add one to each element of m
disp(y1);

% Pass function handle to addtwo
%  to add two to each element of m
disp(y2);

function y = map(f,m)
y = m;
for i = 1:numel(y)
y(i) = f(y(i));
end

y = u + 1;

y = u + 2;
```

This code passes function handles @addone and @addtwo to the function map which increments each element of the matrix m by the amount prescribed by the referenced function. Note that map stores the function handle in the input variable f and then uses f to invoke the function — in this case addone first and then addtwo.

You can use the function fiaccel to convert the function addval to a MEX executable that you can run in MATLAB®. Follow these steps:

1. At the MATLAB command prompt, define and initialize a 3-by-3 matrix:

```m = fi(magic(3));
```
2. Use fiaccel to compile the function to a MEX executable:

```fiaccel addval -args {m}
```
3. Execute the function:

```[y1, y2] = addval_mex(m);
8     1     6
3     5     7
4     9     2

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 11
9     2     7
4     6     8
5    10     3

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 11
10     3     8
5     7     9
6    11     4

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 11
```