I am trying to store some values return from a function. Its returning three values that i want to use in the main function.

    int display()
    {
        int iter;
        int num[4];
        if(dl!=-1)
        {
            for(iter=0 ; iter<=al ; iter++)
            {
                cout<<t[iter]<<", ";
                num[iter]=t[iter];
            }
        }
        else
            cout<<"EMPTY";
            return num[0];
            return num[1];
            return num[2];
    }

This is the function i have. I want to store the all the returned values and i am having a little problem.
It it was one value i would just

if it was only one value this would work fine.

int value=inc.display();

But im a little confuse on how to store all the three values and use them

Recommended Answers

All 8 Replies

You cannot return more than one value in a function. You have to use an array or a pointer.

How would i use the array or pointer. I just want to get the values into the main function somehow

int * Foo()
{
   int * returnValues = new int[what ever size array you want here]
   // do stuff
   return returnValues;
}

// in main
int * getReturnValues = Foo();
 int display(int num[])
 {
    int iter;
    if(dl!=-1)
    {
         for(iter=0 ; iter<=al ; iter++)
        {
            cout<<t[iter]<<", ";
            num[iter]=t[iter];
        }
    }
    else
    cout<<"EMPTY";
    return num;
}

define num in main.

Basically, what you could do is just declare your three variables in main(), since that is where you want them. Then, with the long arm of your function, reach all the way over into main() and manipulate those variables directly. You do this by passing a reference or pointer to those variables to the function. That way, the function has "remote control" from afar of those variables right where they are at, and this makes things much simpler.

First option :

Use global variables.
That way, you can approach them from anywhere in the code.

Second option :

Call the display function with the number of arguments you want it to return
and then use the "new" allocation of an array
- The return value of the function should be int *
If it's always the same number of values you can just declare a new array
the way you did int num[4]

( also you can pass an array in the argument list and fill in the blanks )

Third option :
Declare a static int value inside the function and set it to 0.
Each time you call the function check the static value , increase it by one,
and return the wanted value ( not so useful in your case )

good luck

@ Despairy Use of global variables is slightly discouraged. They can muddle up the names in the file they belong to and can make it hard to find where things are used and declared. Its better to pass arguments to functions than have global variables sitting around and having the functions use them.

I know it's not recommended , just thought he should know that it is an option.
Forgot to mention he should'nt so thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.