Multiple Return();

s0ftware:
Visual C++ 2008 Express

inf0:
Console Project

c0de:
1
2
3
4
5
6
7
8
9
10
11
int Process(int a, int s, int m, int d)
{
	cout << "Addition Process (" << x << "+" << y << ") = " << endl;
	return(a)
	cout << "Subtraction Process (" << x << "-" << y << ") = " << endl;
	return(s)
	cout << "Multiplication Process (" << x << "*" << y << ") = " << endl;
	return(m)
	cout << "Division Process (" << x << "/" << y << ") = " << endl;
	return(d)
}


err0r:
Compiling...
Calculator.cpp
calculator.cpp(20) : error C2146: syntax error : missing ';' before identifier 'cout'
calculator.cpp(22) : error C2146: syntax error : missing ';' before identifier 'cout'
calculator.cpp(24) : error C2146: syntax error : missing ';' before identifier 'cout'
calculator.cpp(26) : error C2143: syntax error : missing ';' before '}'
calculator.cpp(38) : error C2660: 'Process' : function does not take 1 arguments
calculator.cpp(60) : error C2660: 'Process' : function does not take 1 arguments
calculator.cpp(68) : error C2660: 'Process' : function does not take 1 arguments
calculator.cpp(76) : error C2660: 'Process' : function does not take 1 arguments


pr0blem:
Not sure if I'm even doing this right. I tried using void Process(int a, int s, int m, int d) so I could type Process(a); in the int main(); sector, however, it isn't working out.
A function can only return a single value.
Also your return statements need ; after them
Can I ask why you've replaced all 'o's with '0's?
Anywayway, it can only return one value and isn't meant to have parentheses.

So, like this: return a;
but you could do return a*5;

You could try return a, b, c, d; but I doubt that would work.
Umz is correct.
To elaborate further, the 'return' instruction exits the function.

1
2
3
4
5
6
7
int foo(int a,int b)
{
  return a;  // exits function

  a += b;    // so this never executes
  return a;
}


Returning multiple values with a comma (as per chrisname's suggestion) wouldn't work. Also the parenthesis aren't required, but don't really hurt anything.

As for your last 4 errors -- it looks like you're attempting to call this function (which takes 4 arguments) but you're only giving it 1 argument when you call it. You need to supply all 4. From the looks of this function I'm guessing it's doing something much different than you think it is.
Yeah, I tried to compress all 4 functions into one function, and tried calling on each return from the same function.

Before this I had

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void pAddition()
{
	cout << "Addition Process (" << x << "+" << y << ") = " << endl;
}

void pSubtraction()
{
	cout << "Subtraction Process (" << x << "-" << y << ") = " << endl;
}

void pMultiplication()
{
	cout << "Multiplication Process (" << x << "*" << y << ") = " << endl;
}

void pDivision()
{
	cout << "Division Process (" << x << "/" << y << ") = " << endl;
}


and called the functions using
1
2
3
4
pAddition();
pSubtraction();
pMultiplication();
pDivision();


Is there any other way to compress all the void functions into one? and still being able to call on the functions?
umm. sure:

1
2
3
4
5
6
7
void pMath()
{
  cout << "Addition Process (" << x << "+" << y << ") = " << endl;
  cout << "Subtraction Process (" << x << "-" << y << ") = " << endl;
  cout << "Multiplication Process (" << x << "*" << y << ") = " << endl;
  cout << "Division Process (" << x << "/" << y << ") = " << endl;
}


=P

Of course then you won't be able to call each small function individually, because you don't have any of those small functions anymore.

If you want to be albe to call the small functions individually, you can keep them as they are, and make pMath call each of them. Like so:

1
2
3
4
5
6
7
void pMath()
{
  pAddition();
  pSubtraction();
  pMultiplication();
  pDivision();
}

For that job, the best you can do is to use void function and call function by reference,
something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void function(int &,int &,int &,int &,int x,int y);
void main ()  {

	int add,sub,mul,div,x,y;

	cout<<"Enter numbers x,y: "<<endl;
	cin>>x>>y;

	function(add,sub,mul,div,x,y);

	cout<<"Addition: "<<add<<endl;	
	cout<<"Subtraction: "<<sub<<endl;
	cout<<"Multiplication: "<<mul<<endl; 
	cout<<"Division: "<<div<<endl; 

}

void function(int &a,int &s,int &m,int &d,int x,int y) {
a=x+y;
s=x-y;
m=x*y;
d=x/y;
}
Last edited on
Topic archived. No new replies allowed.