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?
i HATE when books dont give you the answer it makes me come to the forum for help. hahaCode:Return(1,2);
what do you guys think is the trigger here? since the return statement wont work.. what will?
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.
Yes.Originally Posted by Hawk1
No.Originally Posted by Hawk1
References.Originally Posted by Hawk1
Uh, do I misunderstand here....? Can't you just have something like this:
I mean, just roughly..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; }
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
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.
I'll give you an example of how you could return two values, although it isn't that pretty.
Why can't he just use my example, wasn't that the assignment ("using references")?
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
He is asking how to return 2 values, your example doesn't do that.
In fact your example doesn't return any values.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks