Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 105,764 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,645 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



function returning two arrays

 
Reply to this topicStart new topic

function returning two arrays, function returning two arrays

prads
post 3 Jul, 2008 - 08:28 AM
Post #1


D.I.C Head

**
Joined: 22 Oct, 2007
Posts: 103


My Contributions


Hello,
I want to write a function that should return 2 arrays of size 10 and type double i.e a[10] and b[10]. Is that even possible. If yes, can somebody show me an example? A very rough prototype of my requirement is shown below:

CODE

double my_function(float x, float y)
{
for(i=0;i<100;i++)
{
//statements
//statements
}
return a[];
return b[];
}

Thanks,
prads
User is offlineProfile CardPM

Go to the top of the page


BetaWar
post 3 Jul, 2008 - 08:49 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,011



Thanked 32 times

Dream Kudos: 675
My Contributions


I am pretty sure it is possible (forgive my ignorance, I don't program in C/C++ much), just not the way you are attempting it. When you call a return it exits the function right after the return is complete, meaning that right now your code it returning a[] and not the second line. What I would try (If I was you) is to combine the 2 arrays into a single, multidimensional array so that it can be returned in a single line of code, then split them apart afterwards.

Something like so:

CODE

return c[2] = [a, b];


Then you can split them outside of the function lik eos:

CODE

r_Val = my_function(3.014, 6.28);
a = r_Val[0];
b = r_Val[1];

(Or however it works in C)

As I said, I am not a pro at C/C++, but that is the basic idea that I would use.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

Mallstrop
post 3 Jul, 2008 - 08:55 AM
Post #3


New D.I.C Head

*
Joined: 19 Jun, 2008
Posts: 30



Thanked 1 times
My Contributions


I don't think you can return 2 objects from a function.

You could concatanate them and return a single array and split it at a later point. Assuming they're arrays of the same type.

A better option might be to make a struct that contains 2 array and return that struct type.
User is offlineProfile CardPM

Go to the top of the page

tortillaboy05
post 3 Jul, 2008 - 09:00 AM
Post #4


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 20


My Contributions


As beta said you can't return two things back to main. However when you create arrays the name is a pointer:

double nums[10];

nums is a constant double pointer, and its pointing to the first element in the array. If you need to input values into the array in your function you need to pass the "array" to your function. When you pass the name of the array to the function your actually passing a pointer. This gives you the power to change the array.

I'm assuming this is what you are trying to do. Declare your two arrays in main and pass them to your functions so you can populate them maybe this will get you started:

void populateArray(double [], double[], ....);

of course your prototype may not look like this because I don't know exatcly what you're trying to do. But that is what your prototype should look like if your passing arrays.

Mallstrops idea is good too, might be the best option, but if it's for a homework assignment you should do exactly what the problem asks. If your doing it just for you, then I'd say a structure would be the best option.

This post has been edited by tortillaboy05: 3 Jul, 2008 - 09:28 AM
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 3 Jul, 2008 - 11:47 AM
Post #5


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 2,048



Thanked 40 times

Dream Kudos: 1425

Expert In: C, C++

My Contributions


Concatenate the two arrays, and add a token to seperate them.

For example, if array1 = "Danny" and array2 = "gabehabe" then array3 = "Danny|gabehabe"

Then, split it again after returning (in your main)

Also, if you only want to edit the actual values stored in that array, you could pass a pointer to array1 and change the actual values being stored, and then your function could return void. smile.gif
User is online!Profile CardPM

Go to the top of the page

Einherjar
post 3 Jul, 2008 - 12:32 PM
Post #6


D.I.C Head

**
Joined: 10 Feb, 2008
Posts: 73


My Contributions


Honestly I would say the easiest way to do it would be to pass two arrays in as parameters and do it that way. For example, if you didn't want to edit the arrays that you send if just send in two empty arrays:

void myfunc(double array1[], double array2[], double result1[], double result2[])

And I believe in C/C++ arrays are automatically pass by reference so this should work? Hopefully tongue.gif
User is offlineProfile CardPM

Go to the top of the page

Tom9729
post 3 Jul, 2008 - 12:32 PM
Post #7


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,387



Thanked 7 times

Dream Kudos: 325
My Contributions


Any code after a call to "return" will not be executed, because "return" exits the function.

Depending on which compiler (and which compiler flags you're using), you should be warned about "unreachable code".

A "double" is a kind of number btw, which stands for double precision. It doesn't mean two variables. smile.gif

If you want to modify two arrays, pass pointers to them as parameters to the function.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 8/21/08 02:23PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month