Thread: Returning Multiple Values

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    Returning Multiple Values

    I've been trying to figure out how to return multiple values from a user defined function back to main. Any ideas?

    int encodeData()
    {
    int inNumOne, inNumTwo, inNumThree, inNumFour,
    calcNumOne, calcNumTwo, calcNumThree, calcNumFour, calcNumAll;
    char inLetter;

    cout << "Enter the data to be encoded: ";
    cin >> inLetter >> inNumOne >> inNumTwo >> inNumThree >> inNumFour;

    calcNumOne = (inNumOne + 7) % 10;
    calcNumTwo = (inNumTwo + 7) % 10;
    calcNumThree = (inNumThree + 7) % 10;
    calcNumFour = (inNumFour + 7) % 10;


    }

    I've been trying to get those values of calcNumOne and the rest back to main so I can ouput them. Ignore inLetter.

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Sure thing honey; this'll work:
    Code:
    #include <iostream>
    using namespace std;
    
    void gimmeStuff(int *a, int *b){
       *a = 5;
       *b = 6;
    }
    
    int main(void){
       int x=0;
       int y=0;
       gimmeStuff(&x,&y);
       cout << "X is now "<<x<<" and Y is now "<<y<<endl;
       return 0;
    }
    Last edited by Krak; 03-08-2005 at 09:35 PM.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Woop?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Another possible way to return multiple pieces of data to a calling function, besides passing multiple parameters to the function by reference, is to return an object (like an instance of a user defined class or struct) or to use the struct as the parameter, passing it by reference of course.
    You're only born perfect.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by Krak
    Sure thing honey; this'll work:
    Code:
    #include <iostream>
    using namespace std;
    
    void gimmeStuff(int *a, int *b){
       *a = 5;
       *b = 6;
    }
    
    int main(void){
       int x=0;
       int y=0;
       gimmeStuff(&x,&y);
       cout << "X is now "<<x<<" and Y is now "<<y<<endl;
       return 0;
    }
    Or you could use references

    Code:
    #include <iostream>
    using namespace std;
    
    void swap(int &var1,int &var2)
    {
         int temp = var1;
         var1 = var2;
         var2 = temp;
    }
    
    int main()
    {
        int abc = 1;
        int def = 2;
        swap(abc,def);
        cout << abc << def << endl;
    }
    I haven't checked that
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Thank you so much Xipher. I was able to implement something similar to that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem returning values in user defined format
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 11-20-2006, 05:20 PM
  2. test to see if multiple values are the same
    By squeaker in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2006, 01:45 AM
  3. multiple values in #define(?) & counter placement
    By bnmwad in forum C Programming
    Replies: 2
    Last Post: 04-30-2006, 06:00 AM
  4. Returning Multiple Values from a function
    By Eddie K in forum C Programming
    Replies: 6
    Last Post: 03-12-2006, 01:18 PM
  5. Returning multiple types w/o a struct
    By Trauts in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2003, 11:04 PM