it's fairly simple, I'd like to output more than one variable from a function to the main program, but I can't think of anyway to get this to happen.
it's fairly simple, I'd like to output more than one variable from a function to the main program, but I can't think of anyway to get this to happen.
i dunno
The simplest way is to put it into a struct or class.
Or, you could pass the arguments by reference and manipulate them there:Code:struct Object { int x; float y; }; Object func() { Object o; o.x = 3; o.y = 4.4; return o; }
Code:void func(int& x, float& y) { x = 3; y = 4.4; } int main () { int x; float y; func(x,y); return 0; }
A class, or struct, is a good way to pass alot of information to a function. If you just want to pass two variables, you just do:
Code:void Output(int x, char ch) { cout<<x<<endl; cout<<ch<<endl; }
Well, there are a few things wrong with your code:
1) It does not work.
2) It does not work.
3) It does not work.
Hope this helps.
Yes I understand that, however, with the function it both 1) takes variables from int main() and 2) requires to output numerous variables. I thought about putting the varaibles in individual functions for transfer, but int main() needed to give the functions new values .
I guess I should have mention I needed to both input and output in the first post...
Not sure I understand...did you look at ygf's code (modified a bit):
Code:void func(int& x, float& y) { x = 3; y = 4.4; } int main () { int x=1; float y=2; cout<<"X:\t"<<x<<endl; cout<<"Y:\t"<<y>>endl; func(x,y); cout<<"X:\t"<<x<<endl; cout<<"Y:\t"<<y<<endl; return 0; }
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
....or if the data you want to return is all the same type, you can just pass your function an array:
Code:#include<iostream> using namespace std; void func(int a[]) { a[0]=10; a[1]=30; } int main() { int a[2]={0}; func(a); for(int i=0; i<3; i++) cout<<a[i]<<endl; return 0; }
You don't have to pass variables to a function, just declare them inside the function:
7stud,Code:void Output() { int x; cin>>x; cout<<x; } //x does not exist anymore
Code:for(int i=0; i<2; i++)
Well, there are a few things wrong with your code:
1) It does not work.
2) It does not work.
3) It does not work.
Hope this helps.
...or if the data types you want to return are different you could do this:
Code:#include<iostream> using namespace std; void func(int* i, double* d) { *i = 50; *d = 2.75; } int main() { int* i; int my_int = 0; i=&my_int; double* d = 0; double my_double = 0; d = &my_double; func(i, d); cout<<*i<<endl<<*d<<endl; return 0; }
funkydude9,
Please read the question more carefully. You certainly can't return statically allocated variables from a function.
Huh? I didn't say you could. From his question, it sounded like he didn't want to pass the variables to the function....funkydude9,
Please read the question more carefully. You certainly can't return statically allocated variables from a function.
Last edited by funkydude9; 08-10-2003 at 08:58 PM.
Well, there are a few things wrong with your code:
1) It does not work.
2) It does not work.
3) It does not work.
Hope this helps.
the first example 7stud gave (after my second post) is very close to what I want. In the same function I input and output the variables and during the function's run, the varaibles change, and so I must update the values of the variables in int main() to match the ones in the function one the function finished, and the function (once run twice) will have to have the update variables.
Quick eg:
Code:function(int a, int b){ a * 2; b / 4; } int main() { int a, b; a = 1; b = 8; function(a,b); // code to get variable values from function() cout << a << b <<endl; function(a,b); // code to get variable values from function() cout << a << b <<endl; return 0; }
Just pass them by refrence like so:
There's no need for you to call it twice. Just output it once before you call the function, then output it after.Code:void function(int &a, int &b){ a = a* 2; b = b/ 4; }
::EDIT::
whoops! Fixed your function, too.
Last edited by funkydude9; 08-10-2003 at 09:05 PM.
Well, there are a few things wrong with your code:
1) It does not work.
2) It does not work.
3) It does not work.
Hope this helps.
I would want to call it twice to update the variables
b should have been = to 16, sorry, but
wanted output from eg program:
24
41
reason:
first line the function would doulbe a (1-->2) and devide b by a quarter (16 --> 4), ten output the value of a then b with no space giving 24, then repeat the equation with the new values (2 --> 4) and (4--> 1)
the comments should be replace with what ever code is need to grab the variables from the function, and yes i did modify the function and test the program, it gave:
116
116
Last edited by Dark Nemesis; 08-10-2003 at 09:22 PM.
Use recursion (calling the same function within a function) and then use an if statement to check if you have the desired values.would want to call it twice to update the variables
Well, there are a few things wrong with your code:
1) It does not work.
2) It does not work.
3) It does not work.
Hope this helps.