Im usind Dev-C++ and it allows to return more than 1 value :eek:. ok, so here's my code :

#include <iostream>
using namespace std;

int intFirst, intSecond, intThird;

int BreakUp(int intNumb) {
 
 intFirst = intNumb / 100;
 intSecond = (intNumb - (intFirst * 100)) / 10;
 intThird = intNumb - ((intFirst * 100) + (intSecond * 10)); 
 return(intFirst, intSecond, intThird);    
}
int main()
{
    int a;
    cout<<"Enter number between (0-999) to break up: ";
    cin>>a;

    BreakUp(a); //calling
    cout<<"=========================="<<endl;
    cout<<"First integer: "<<intFirst<<endl;
    cout<<"Second integer: "<<intSecond<<endl;
    cout<<"Third integer: "<<intThird<<endl;
    system ("PAUSE"); //I know, so just ignore it=)
    return(0);
}

Recommended Answers

All 4 Replies

>return(intFirst, intSecond, intThird);
This doesn't actually return three values, it just seems that way because intFirst, intSecond, and intThird are global. What this expression actually does is evaluate (and subsequently ignore) all but intThird, which it then returns. So only intThird is returned, but because all of them are global, you can still access them in main and the values won't disappear.

If you want to write a function that returns more than one value you have to pass parameters by reference or pointer to the function so that it can populate it with the needed information. Example in c++.

void BreakUp(int intNumb, int& first, int& second,int &third) {
 
 first = intNumb / 100;
 second = (intNumb - (intFirst * 100)) / 10;
 third = intNumb - ((intFirst * 100) + (intSecond * 10)); 
}

int main()
{
<snip>
      BreakUp(a, intFirst, intSecond, intThird);
<snip>
}

Thnx for fast replies :) Reference Parameters make sense but i'm still kinda confused with return() statement...

so for example i have a function :

// checks whatever number is prime or not and returns bool
int IsPrime(int Number)
{
    bool blPrime;
    int DivideBy = 2;
    while (Number%DivideBy!=0)
      DivideBy++;
    if (DivideBy==Number)
      blPrime = true;
    else
      blPrime = false;
   return (blPrime);   
}

so if i want to use it in my int main() function how should I declare bool blPrime?? before i simply made bool blPrime global variable.

example:

int main()
{
    int a;
    cout << "Enter number to check for Prime: ";
    cin >> a; 
    IsPrime(a);
    if (blPrime == true){
     cout<<"Prime"<<endl;}
    else if (blPrime == false){
     cout<<"Not Prime"<<endl; }
    return(0);
}

There are two ways you can achieve the feat: either call the function itself inside the if statement or store the bool returned by the function in a bool variable and check that variable in the if statement.

Eg.

bool is_prime = isPrime( number ) ;
if( is_prime == true)
   cout << "Prime" ;
else
   cout << "Not prime" ;

//OR
if( isPrime( number ) == true )
   cout << "Prime" ;
else
  cout << "Not prime" ;

Hope it helped, bye.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.