Sending a two values to main from a function?

This is a discussion on Sending a two values to main from a function? within the C++ Programming forums, part of the General Programming Boards category; I got main() and a second user defined function. In my second user defined function there are two double values ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    20

    Sending a two values to main from a function?

    I got main() and a second user defined function. In my second user defined function there are two double values I need to pass to main. How do I do it (using addressing).

    Note: I cannot use global variables.

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you don't pass anything to main(). main() is the entry point for your program. don't call it.

    I presume you mean you want to return 2 values from your function to main().

    There are a few different ways to do this. If you're using C look up pointers, if you're using C++ look up references.

    give it a go and then post if you get stuck.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Quote Originally Posted by ChaosEngine
    you don't pass anything to main(). main() is the entry point for your program. don't call it.

    I presume you mean you want to return 2 values from your function to main().

    There are a few different ways to do this. If you're using C look up pointers, if you're using C++ look up references.

    give it a go and then post if you get stuck.

    I am coding in c++.

    Can you provide a link to these "references"? Is that the only way to go about it? I was sort of told to use something called address.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You can only return one "thing" in a return statement. However, the thing you return can contain multiple values. For instance, you can return an array filled with values. Actually, if you initially send an array to a function, you don't even have to return anything: you can just assign values to the elements of the array, and they will be available back in main(). Or, you can return a struct filled with values.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    7stud : That does sound like a good way to go about it, but I do not think i could get away with that.

    dwks: I read the reference. I just cannot see how that would work. Could you perhaps give me an example?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    7stud : That does sound like a good way to go about it, but I do not think i could get away with that.
    I don't know of any programming languages that let you return more than one thing in a return statement, so good luck.
    Last edited by 7stud; 12-05-2005 at 04:44 AM.

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Zeratulsdomain
    7stud : That does sound like a good way to go about it, but I do not think i could get away with that.

    dwks: I read the reference. I just cannot see how that would work. Could you perhaps give me an example?
    Thats how its done.

    Why don't you show us what you have?

    Look up returning a struct, or passing a struct by reference (addressing). Both would work. So would using an array.
    Last edited by Dae; 12-04-2005 at 06:31 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Quote Originally Posted by Dae
    Look up returning a struct, or passing a struct by reference (addressing). Both would work. So would using an array.
    Are you talking about the reference link dwks gave me? I did look at that one.... I dont know why, but I can never find anything on google about c++, I could not even find this site (good thing it was one of the two bookmarks I have in explorer.) Anyway, the reference, how would that work? Do I put the reference in the parentheses of my function or something?

    Quote Originally Posted by Dae
    Why don't you show us what you have?
    Code:
    double defEstimatedTuition (double x, double y)
    {
    	double OneYear, TwoYear;
    	OneYear = (y * x) + y;
    	TwoYear = (OneYear * x) + OneYear;
    	
    	//cout << OneYear << "  " << TwoYear;
    	return (OneYear, TwoYear);
    }
    That does not work (obviously). But you get the point. I want the valies of OneYear and TwoYear in main.

  10. #10
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Use a struct or array!

    Here is how you could return two variables, inside of a struct. Its inefficient and doesnt use references ("addressing") though.

    Code:
    struct SSome
    {
    	double OneYear;
    	double TwoYear;
    };
    
    SSome defEstimatedTuition (double x, double y)
    {
    	SSome something;
    
    	something.OneYear = (y * x) + y;
    	something.TwoYear = (something.OneYear * x) + something.OneYear;
    
    	return something;
    }
    
    int main()
    {
    	SSome another;
    	another = defEstimatedTuition(10.0f, 5.0f); /*assign another to the return value of defEstimatedTuition*/
    
    	cout << another.OneYear << "  " << another.TwoYear;
    }
    See if you can change it to use a reference to the struct instead of returning one now.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by 7stud
    I don't know of any programming languages that let your return more than one thing in a return statement, so good luck.
    Python , PERL, PHP(I think).
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Python
    The split function splits a full pathname and returns a tuple containing the path and filename.
    We saw that lists and strings have many common properties, such as indexing and slicing operations. They are two examples of sequence data types. Since Python is an evolving language, other sequence data types may be added. There is also another standard sequence data type: the tuple.

    A tuple consists of a number of values separated by commas...
    It sounds like a tuple is similar to an array.


    Same for Perl:
    This multi-valued return value is a point worth pondering. It means that some Perl functions will return scalars and others will return arrays. Even more interesting, the same function can sometimes return a scalar, and sometimes an array.

    When scalars are used, functions are said to be in a "scalar context". When arrays or lists are used, functions are said to be in a "list context".
    PHP(I think).
    You can't return multiple values from a function, but similar results can be obtained by returning a list.
    http://us3.php.net/manual/en/functio...ing-values.php
    Last edited by 7stud; 12-05-2005 at 06:13 AM.

  13. #13
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Code:
    double defEstimatedTuition (double x, double y)
    {
    	double OneYear, TwoYear;
    	OneYear = (y * x) + y;
    	TwoYear = (OneYear * x) + OneYear;
    	
    	//cout << OneYear << "  " << TwoYear;
    	return (OneYear, TwoYear);
    }
    Try something like:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void outsidefunction(double& x, double &y);
    
    int main()
    {
        double x=0,y=10;
    
        outsidefunction(x,y);
    
        cout<<"X: "<<x<<" Y: "<<y;
        //should output "X: 20 Y: 10"
    }
    
    void outsidefunction(double& x, double& y)
    {
        x=y*2;
    }
    the & after the type, means use the data in the function it was called from, not just the function it was called to.
    Last edited by Wraithan; 12-05-2005 at 06:29 AM.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    That does not work (obviously). But you get the point. I want the valies of OneYear and TwoYear in main.
    You can pass the function two additional arguments called OneYear and TwoYear which you declare in main():
    Code:
    int main()
    {
    
    	double OneYear = 0;
    	double TwoYear = 0;
    	
    	return 0;
    }
    Then you can put a little '&' symbol just to the right of their corresponding parameter variables in the function definition:
    Code:
    void defEstimatedTuition (double x, double y, double& One, double& Two)
    If you do that, then inside the function you can assign values to the variables One and Two, and those values will be assigned to OneYear and TwoYear back in main(), so you don't have to return anything from your function.
    Last edited by 7stud; 12-05-2005 at 07:22 AM.

  15. #15
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    This is where a std:air comes in handy.

    Code:
    std::pair<double, double> foo() {
        double x = 3.0, y = 2.9;
    
        return make_pair(x,y);
    }
    
    int main() {
        std::pair<double, double> bah;
    
        bah = foo();
    
        std::cout << bah.first << ' ' << bah.second << std::endl;
    
        return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing values from function to main variables
    By ERJuanca in forum C Programming
    Replies: 18
    Last Post: 06-12-2009, 07:13 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 04:14 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21