Sending more then one result from a function to main

How to get 2 or more result from a function to main?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int *values(int k,int p)
{  
	int *ptr = NULL;
	int interest = k*(p / 100);
	int credit = interest + k;
	int z[2]; z[0] = interest; z[1] = credit;
	ptr = z;

	return ptr;


}



int main()
{ 
	
	int credit = 12000; int interest = 10;
	int *Ptr = values(credit, interest);

	for (int i = 0; i < 2; i++)
	{
		cout << *(Ptr+i) << endl;
	}
Last edited on
That's not going to work. z[2] is a local variable. It goes out of scope when values() exits. Therefore ptr is not valid.

Look into passing credit and interest by reference.

Note: You're doing integer arithmetic. Probably not what you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

double values (double & credit, const double & rate)
{   double interest;

    interest = credit * (rate / 100);
	credit += interest;
	return interest;
}

int main()
{   double credit = 12000; 
    const double rate = 10;
    double interest;
    
    interest = values (credit, rate);
    cout << credit << endl;
    cout << interest << endl;
}


But i want to calculate credit and interest and both results send to main because the whole amount of credit is credit plus interests.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
double *values(double &c, double &it)
{  
	double Interests = c*(it / 100);
	double Credit = Interests + c;
	double z[2];  z[0] = Credit; z[1] = Interests;
	double *ptr = z;
	return ptr;

}



int main()
{ 
	
	double credit = 11000.0, interest = 10.0;
	double *Ptr = values(credit, interest);
	
	for (int i = 0; i < 2; i++)
	{
		cout << Ptr[i] << endl;
	}



	return 0;
}
this way i get the first amount I get credit but the second value is something like an address
Lines 5-7: You're ignoring what I pointed out about z[] going out of scope. At line 7, z[] and ptr are NO LONGER VALID.

Line 16: You're trying to use interest for two different things. The interest rate and the amount of interest accrued. Not a good idea. This is why I introduced rate in the sample I posted.

Line 1: c (credit) is passed by reference. If you were to update c within values(), the change would be reflected in the caller (main). Same goes for interest (it).

AAAAAAA i get it now sorry for that :D but how would you do it in your post u just return one value let's forget about the example i posted think about any other situation where you have to send two variables or results to main how would you do that?
This is the easiest way :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void changed_Values(double &c, double &p)
{  
	p = c*(p / 100);
	c = p + c;



}

int main()
{ 
	double credit= 10000.0, interest = 10.0;
	changed_Values(credit, interest);
	cout << credit << " " << interest << endl;




	return 0;
}
Last edited on
As I mentioned before, you're still using interest for two different purposes. Not a good idea.

Suppose you want to call changed_values() a second time. interest no longer has the original rate in it. As I suggested before, rate should be a const value.
 
  const double rate = 10.0;
.
This means of course you're going to need either make rate a global, or pass a third argument to changed_values.

I get it and when i need interest again the variable is changed , but it was only a random name and random purpose :D but i know what u talking in programming u need to consider everything :D
You are a wise man Abstraction :D
Topic archived. No new replies allowed.