# Have a function return a complex number in C?

1. Jun 21, 2013

### denjay

So I wrote a program that has one main function call two other functions in an equation and then calculates a value. The problem is the numbers I need returned from those two other functions need to stay in their complex form. Using double() I get an error when trying to return that. Is there any other function type that can return the numbers in their complex form?

Note: the program is in C.

2. Jun 21, 2013

### Staff: Mentor

I think I would use a pair or a struct, with two doubles inside in both cases.

Edit: Oh, I didn't know C can handle complex numbers like that.

Last edited: Jun 21, 2013
3. Jun 21, 2013

### Staff: Mentor

I would use the complex numbers that are part of the C99 standard and declare the function as double complex.

4. Jun 26, 2013

### denjay

I'm still having this problem so I'll post an abridged part of my code.

I took out a lot but kept the relevent parts.

The loop acts to calculate rn[1] which is the value I need to return. My thought is that the function type is wrong or the variable type is wrong, or that C just doesn't return a complex number?

Code (Text):
#include "cplx.h"

...

double _Complex rnw(double P[], double C[], double x)
{
struct cplx f[20000], an[20000], rn[20000], fnmax, f1, rn1;

...

for (n=nmax; n>=2; n--) {
an[n-1]=cdiv( cdiff (f[n-1],f[n]),csum(f[n-1],f[n]) );
rn[n-1]=cmult( cplxexp( cmult(makecplx(0,-2.*C[5]*d[n-1]),f[n-1])), cdiv( csum(rn[n],an[n-1]),csum(cmult(rn[n],an[n-1]),makecplx(1.0,0) )));
}
return rn[1];
}

5. Jun 26, 2013

### Staff: Mentor

Where do you set the type of rn[]? Is it double _Complex?

6. Jun 26, 2013

### Staff: Mentor

The code does not follow C99 standards at all, as far as I can see, anyway. Plus your return statement is in the middle of a for(...) loop. C99 is the point at which complex support became part of standard C.

The arrays (like rn[]) should be declared as the very same data type as the function: _Complex. I am guessing struct cplx is supposed to be the same datatype as _Complex. But I don't know.

I highly recommend that you follow C standards. Compile to show all warnings, a correct program NEVER compiles with warnings. Even if you can run a program compiled with warnings and it appears to work. This is called 'programming by accident'.

7. Jun 27, 2013

### Staff: Mentor

It is outside, the indentation is a bit confusing.

Well, things like 'unsigned int numberofwhatever=10; for(int i=0; i<numberofwhatever; i++)" (where the first one is hidden somewhere in an object in a completely different file) give a warning about the comparison between signed and unsigned integers (depends a bit on the compiler), but it does not 'work by accident', it just works.
I think I even saw that warning with "for(int i=0; i<10; i++)" once.

8. Jun 27, 2013

### Staff: Mentor

for(int i=0; i<10; i++) is legal C99 - the scope of the variable i is the duration of the loop. And you are correct - the snippet confused me - the return value is in a reasonable place.