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.
Printable View
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.
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.
Quote:
Originally Posted by ChaosEngine
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.
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.
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?
I don't know of any programming languages that let you return more than one thing in a return statement, so good luck.Quote:
7stud : That does sound like a good way to go about it, but I do not think i could get away with that.
Thats how its done.Quote:
Originally Posted by Zeratulsdomain
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.
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
Quote:
Originally Posted by Dae
That does not work (obviously). But you get the point. I want the valies of OneYear and TwoYear in main.Code:double defEstimatedTuition (double x, double y)
{
double OneYear, TwoYear;
OneYear = (y * x) + y;
TwoYear = (OneYear * x) + OneYear;
//cout << OneYear << " " << TwoYear;
return (OneYear, TwoYear);
}
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.
See if you can change it to use a reference to the struct instead of returning one now.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;
}
Python , PERL, PHP(I think).Quote:
Originally Posted by 7stud
It sounds like a tuple is similar to an array.Quote:
Quote:
Python
Quote:
Quote:
The split function splits a full pathname and returns a tuple containing the path and filename.
Quote:
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...
Same for Perl:
Quote:
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".
Quote:
PHP(I think).
Quote:
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
Try something like:Code:double defEstimatedTuition (double x, double y)
{
double OneYear, TwoYear;
OneYear = (y * x) + y;
TwoYear = (OneYear * x) + OneYear;
//cout << OneYear << " " << TwoYear;
return (OneYear, TwoYear);
}
the & after the type, means use the data in the function it was called from, not just the function it was called to.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;
}
You can pass the function two additional arguments called OneYear and TwoYear which you declare in main():Quote:
That does not work (obviously). But you get the point. I want the valies of OneYear and TwoYear in main.
Then you can put a little '&' symbol just to the right of their corresponding parameter variables in the function definition:Code:int main()
{
double OneYear = 0;
double TwoYear = 0;
return 0;
}
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.Code:void defEstimatedTuition (double x, double y, double& One, double& Two)
This is where a std::pair 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;
}