Using the C Math Library    

Returning an Array 

Before you pass an array to the return statement in your function, you must pass that array to mlfReturnValue(). mlfReturnValue() makes the array a temporary array. Your function can therefore return a temporary array just like each function in the MATLAB C Math Library does.

Prototype:

Sample Call from Template:

return mlfReturnValue(local_return_value);

Argument and Return for mlfReturnValue( )

Pass the array that your function returns (an mxArray*) to mlfReturnValue(). The array is a bound array when it is passed to mlfReturnValue() and is typically the result of an assignment made within the function or the value of an output argument set by a function call.

mlfReturnValue() makes the array a temporary array and returns the same array. You can nest the call to mlfReturnValue() in the return statement for your function.

You do not need to call mlfReturnValue() if you are writing a function that does not return a pointer to an array (in the same way that the main() template doesn't call mlfReturnValue()).

Changing Bound Arrays to Temporary Arrays

mlfReturnValue() changes the bound state of the array passed to it to temporary. You then pass that array to the return statement.

Handling Return Values

By marking the returned array temporary, you ensure that a call to your function can be nested as an argument to another function without leaking memory.


 Restoring the Previous Context Example - Managing Array Memory (ex2.c)