Multiple function output

This is a discussion on Multiple function output within the C++ Programming forums, part of the General Programming Boards category; it's fairly simple, I'd like to output more than one variable from a function to the main program, but I ...

  1. #1
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110

    Multiple function output

    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.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    i dunno

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    The simplest way is to put it into a struct or class.
    Code:
    struct Object {
      int x;
      float y;
    };
    
    Object func() {
      Object o;
      o.x = 3;
      o.y = 4.4;
      return o;
    }
    Or, you could pass the arguments by reference and manipulate them there:
    Code:
    void func(int& x, float& y) {
      x = 3;
      y = 4.4;
    }
    int main () {
      int x;
      float y;
      func(x,y);
      
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    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.

  5. #5
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    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...

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ....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;
    }

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    You don't have to pass variables to a function, just declare them inside the function:
    Code:
    void Output()
    {
         int x;
         cin>>x;
         cout<<x;
    } //x does not exist anymore
    7stud,
    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.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ...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;
    }

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    funkydude9,

    Please read the question more carefully. You certainly can't return statically allocated variables from a function.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    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....
    Last edited by funkydude9; 08-10-2003 at 09: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.

  12. #12
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    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;
    }

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    Red face

    Just pass them by refrence like so:
    Code:
    void function(int &a, int &b){
    a = a* 2;
    b = b/ 4;
    }
    There's no need for you to call it twice. Just output it once before you call the function, then output it after.
    ::EDIT::
    whoops! Fixed your function, too.
    Last edited by funkydude9; 08-10-2003 at 10: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.

  14. #14
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    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 10:22 PM.

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    would want to call it twice to update the variables
    Use recursion (calling the same function within a function) and then use an if statement to check if you have the desired values.
    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.

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

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

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