1

I've read some posts regarding my question. But I'm still not sure of the following:

I've generated Matlab's c coder to generate the c version of the findpeaks function. However, the functions generated all start with void or static void. Does that mean the functions won't return anything?

Thanks....

0

3 Answers 3

1

Functions generated by the C coder return void, so in fact nothing, but the values returned by the matlab function are 'returned' via pointers or arrays which come last in the arguments and which have their value set in the generated C code. This is done like this because matlab functions can return multiple values which you cannot do straightforward in C except by returning e.g. a struct or so.

Suppose your matlab function is

function [x,y] = Foo(a)
  x = a + 1.0
  y = 5 * ones(1,3)

then the generated C function declaration should be something like

void Foo(real_T a, real_T *x, real_T y[3]);

and if you call it like

real_T x;
real_T y[3];
Foo(0.0, &x, y);

then x will be set to 1.0 and y will be an array with all elements set to 5.

Sign up to request clarification or add additional context in comments.

2 Comments

So if I call the Foo function in another c function... I could still retrieve the value of x as 1.0?
If you pass 0 as first argument then x is set to 1, does't matter where you call it.
1

If a function looks like this:

  void f(void);

then it can't return anything via its return value, so you can't say things like:

  int n = f();

However, a function can also return values via its parameter list, using pointers:

  void f( int * p ) {
     * p = 42;
  }

  .....

  int n;
  f( & n );    // n now contains 42

or by setting global variables.

Comments

0

If the return type of a function is void, the function doesn't return any value.

According to Wikipedia's void type page:

The void type, in several programming languages derived from C and Algol68, is the type for the result of a function that returns normally but does not provide a result value to its caller. Usually, such functions are called for their side effects, such as performing some task or writing to their output parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.