Closed Thread
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 35

Thread: HMM. Tricky question, returning references for 2 values, not 1

  1. #1
    Hawk1 is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    70
    Rep Power
    0

    HMM. Tricky question, returning references for 2 values, not 1

    Welll i decided to learn how to do references tonight, and for this question in the book it says "can you think of a program that will take arguments of an integer and a double using references. your added function should display the square of both values.
    *hints*
    test in main, by asking user to input a number and its double and be sure to print out the square to both values.


    this got me thinking....
    you can only return one value so i guess thats the reason for a reference.... but something like this wont work will it?

    Code:
     Return(1,2);
    i HATE when books dont give you the answer it makes me come to the forum for help. haha

    what do you guys think is the trigger here? since the return statement wont work.. what will?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    LukeyJ is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    92
    Rep Power
    13

    Re: HMM. Tricky question, returning references for 2 values, not 1

    In C++ I haven't found a way. One way you can do it, is call 2 functions:

    root_1_var = root_of_var(raw_double);
    root_2_var = root_of_var(raw_int);

    Where root_of_var is an overloaded function that can accept arguments of either int or double. If you just mean double, as in twice the value of the first. Just call the function twice and set in 2 different variables and output them.

    That's the best advice I can offer.

  4. #3
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42

    Re: HMM. Tricky question, returning references for 2 values, not 1

    Quote Originally Posted by Hawk1
    you can only return one value so i guess thats the reason for a reference....
    Yes.

    Quote Originally Posted by Hawk1
    but something like this wont work will it?
    No.

    Quote Originally Posted by Hawk1
    what will?
    References.

  5. #4
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: HMM. Tricky question, returning references for 2 values, not 1

    Uh, do I misunderstand here....? Can't you just have something like this:

    Code:
    #include <cmath>
    #include <iostream>
    
    using namespace std;
    
    void squares(int &myInt, double &myDouble)
    {
    myInt = sqrt(myInt);
    myDouble = sqrt(myDouble);
    }
    
    int main(int argc, char* argv[]
    {
    int a = 10; // Whatever
    double b = 40.97; // Whatever
    
    squares(a, b);
    
    cout << "Square a = " << a << endl;
    cout << "Square b = " << b << endl; 
    
    return 0;
    }
    I mean, just roughly..
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  6. #5
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: HMM. Tricky question, returning references for 2 values, not 1

    It can be possible to return more than a primitive from a function, but when you think about it, you can see how it can be wasteful to do so.

    In standard C, for example, there is the function div that returns a div_t (which is a structure with multiple members). Similarly, user-defined functions can do the same thing.

    But imagine if you had a bigger struct that contained a lot of members. You create a temporary, do something with it, and return it, at which point it is copied to assign it. That's a lot of memory manipulation. Contrast with passing a pointer or a reference to the existing object in the calling function and merely modifying members in place.

  7. #6
    Join Date
    May 2008
    Posts
    2,126
    Blog Entries
    1
    Rep Power
    33

    Re: HMM. Tricky question, returning references for 2 values, not 1

    I'll give you an example of how you could return two values, although it isn't that pretty.

  8. #7
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: HMM. Tricky question, returning references for 2 values, not 1

    Why can't he just use my example, wasn't that the assignment ("using references")?
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  9. #8
    Join Date
    May 2008
    Posts
    2,126
    Blog Entries
    1
    Rep Power
    33

    Re: HMM. Tricky question, returning references for 2 values, not 1

    He is asking how to return 2 values, your example doesn't do that.

    In fact your example doesn't return any values.

  10. #9
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: HMM. Tricky question, returning references for 2 values, not 1

    Quote Originally Posted by Hawk1 View Post
    "can you think of a program that will take arguments of an integer and a double using references.

    [...]

    what do you guys think is the trigger here? since the return statement wont work.. what will?
    He didn't really ask for returning anything. He asked how to do it without returning anything, and the assignment asked him to use references.

    But fine, give us an example, maybe I'll do one too.
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  11. #10
    Join Date
    May 2008
    Posts
    2,126
    Blog Entries
    1
    Rep Power
    33

    Re: HMM. Tricky question, returning references for 2 values, not 1

    Hmmm, if you reread his post it's pretty evident what he is asking about.

    Something that your snipped doesn't solve or even relate to.

Closed Thread
Page 1 of 4 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Interview question - returning a string of matches
    By Roger in forum Programming Theory
    Replies: 7
    Last Post: 08-24-2010, 07:17 AM
  2. creating a +/- poll (returning multiple values)
    By SuchTheFool in forum PHP Development
    Replies: 5
    Last Post: 06-08-2010, 03:50 AM
  3. returning values
    By Hot_Milo23 in forum Python
    Replies: 8
    Last Post: 10-13-2009, 07:27 AM
  4. Very simple question about references
    By Shinka in forum C# Programming
    Replies: 9
    Last Post: 03-01-2009, 09:49 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts