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

Join 132,679 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,187 people online right now. Registration is fast and FREE... Join Now!




functions

 
Reply to this topicStart new topic

functions

gogole
post 19 Jul, 2007 - 10:36 AM
Post #1


D.I.C Head

Group Icon
Joined: 17 Jul, 2007
Posts: 131



Dream Kudos: 25
My Contributions


is there anyway that i can return more than one variable in a function?
User is offlineProfile CardPM

Go to the top of the page

PennyBoki
post 19 Jul, 2007 - 10:48 AM
Post #2


system("revolution");

Group Icon
Joined: 11 Dec, 2006
Posts: 2,009



Thanked 5 times

Dream Kudos: 500

Expert In: Java,C++,C

My Contributions


yes there is by using a pointer to a structure which holds more variables.
I think...
User is offlineProfile CardPM

Go to the top of the page

Topher84
post 19 Jul, 2007 - 11:30 AM
Post #3


D.I.C Head

Group Icon
Joined: 4 Jun, 2007
Posts: 232



Dream Kudos: 25
My Contributions


QUOTE(PennyBoki @ 19 Jul, 2007 - 11:48 AM) *

yes there is by using a pointer to a structure which holds more variables.
I think...


You could pass the variables by reference which will not make copies and change the variable itself throughout the program.... its just a "way" of doing it but it could mess up the variables if they are used in other parts.

data_type function_name(&referenced_variable);
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 19 Jul, 2007 - 11:42 AM
Post #4


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


Maybe you could make a struct? Or declare the variables global, but this isn't a great solution, it's just avoiding the problem.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 19 Jul, 2007 - 03:18 PM
Post #5


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


Technically, only one object can be returned from a function. The way to get around that is to use the right object. Build a class, and return an instance of it. Build a struct and have your various components inside. Use a copy of an array. You could also build a string of concatenated variables, and parse it once returned.
User is offlineProfile CardPM

Go to the top of the page

Xing
post 19 Jul, 2007 - 10:59 PM
Post #6


D.I.C Addict

Group Icon
Joined: 22 Jul, 2006
Posts: 723



Thanked 2 times

Dream Kudos: 1575
My Contributions


QUOTE(Amadeus @ 20 Jul, 2007 - 04:48 AM) *

Technically, only one object can be returned from a function. The way to get around that is to use the right object. Build a class, and return an instance of it. Build a struct and have your various components inside. Use a copy of an array. You could also build a string of concatenated variables, and parse it once returned.

Actually it's possible to return multiple values from function. One possible way would be to return std::pair if you want to return 2 values from the function. If you want multiple values to be returned from the function then you can use The Boost Tuple Library
http://www.boost.org/libs/tuple/doc/tuple_users_guide.html
which is a portable solution. It is supposed to be added in future version of C++.
Sample Code
CODE

#include <utility>

std::pair<int,int> foo()
{
   return std::make_pair(10,20);
}


int main()
{
    std::pair<int,int> pr = foo();
}


This post has been edited by Xing: 19 Jul, 2007 - 11:23 PM
User is offlineProfile CardPM

Go to the top of the page

zyruz
post 20 Jul, 2007 - 12:31 AM
Post #7


New D.I.C Head

*
Joined: 13 Aug, 2005
Posts: 31


My Contributions


you can do this with pointer's, but then you need to add the pointers when you call the function, like:
CODE
#include <iostream>

using namespace std;

void addone(int *, int * );

int main()
{
    int a=5;
    int b=9;
    cout << "a: " << a << "  b: " << b << "\n";
    addone(&a, &b);
    cout << "a: " << a << "  b: " << b << "\n";
    system("pause");
}

void addone(int *first, int *second)
{
     (*first)+=1;
     (*second)+=1;
}



You can use a struct like:
CODE
#include <iostream>
using namespace std;

struct two
{
    int a;
    int b;
};

two func()
{
    two temp = {5,4};
    return temp;
}

int main()
{
    two test = func();
    cout << test.a << "  " << test.b << "\n";
}


or you can use a vector. the advantice of this is that you dont need to know how manny variables you are going to return.
like:(C++ only)
CODE
#include <iostream>
#include <vector>

using namespace std;

vector<int> fillvec(int a)
{
    vector<int> temp;
    for (int i=0; i<a; i++)
    {
        temp.push_back(i);
    }
    return temp;
}

int main()
{
    vector<int> temp;
    
    temp = fillvec(5);
    
    for(int i=0; i<temp.size(); i++)
    {
        cout << temp[i] << "\n";
    }

}


Zy

This post has been edited by zyruz: 20 Jul, 2007 - 12:33 AM
User is offlineProfile CardPM

Go to the top of the page

gogole
post 20 Jul, 2007 - 06:39 AM
Post #8


D.I.C Head

Group Icon
Joined: 17 Jul, 2007
Posts: 131



Dream Kudos: 25
My Contributions


thanks guys you've been of great help.kudos to all of ya.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:34AM

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