Printing the output of a function in int main()

Hi guys.
I have a problem with functions.

To put it simply. If I create a void function that does something when I call it, and I want to print (i.e. cout) its result in int main() , I can NOT. I get an error. For example:

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
28
29
30
31
32
33
34
35
#include <iostream>
#include <cmath>
using namespace std;

struct fraction
{
	 int numerator;
	 int denominator;
};


void sum(int x, int y, int z, int k)
{
    double f3;
    f3 = ((double)x/y)+((double)z/k);
    cout << "sum of the two fractions = ";

}


int main()
{

fraction  f1, f2;

cout << "enter the numerator and demominator of the first  fraction: ";
cin >> f1.numerator >> f1.denominator;

cout << "enter the numerator and demominator of the second fraction: ";
cin >> f2.numerator >> f2.denominator;

cout << sum(f1.numerator, f1.denominator, f2.numerator, f2.denominator);

return 0;
}



Now, if you run this, you will get an error regarding
 
cout << sum(f1.numerator, f1.denominator, f2.numerator, f2.denominator);



The fix is simple.

1) You can replace the line that is causing the error with:
 
sum(f1.numerator, f1.denominator, f2.numerator, f2.denominator);

i.e. DELETING "cout"
And, inside the definition of the function sum() , at the end of it, write:
 
cout << f3;



OR


2) replace the function type of void with double .
And then at the end of the function, write return f3; .
And now, you can write "cout << sum(....)" without any errors.




But, I do not want to do either of these compromises! I want to keep the function as a void type and be able to cout the function in int main() .

How can I do that?? Or is it the case that if you have a function of type void , then you simply can not cout it?
Last edited on
Functions whose return type is void do not return values; they do not produce any result.

It's important to write functions that do exactly one thing. If I write a function named "sum", then it should sum its arguments and give me the result, and do nothing else. It shouldn't sum its arguments and then print something to one particular stream, or modify some unrelated global state, or cook me dinner.

1
2
3
4
5
6
7
8
9
10
11
12
# include <iostream>

struct rational { int p = 1, q = 1; }; 

double as_real(rational r) 
{ return static_cast<double>(r.p) / r.q; }

double sum(rational a, rational b) 
{ return as_real(a) + as_real(b); }

int main() 
{ std::cout << "1/3 + 2/6 = " << sum(rational{1, 3}, rational{2,  6}) << '\n'; }
Last edited on
Thank you for the clarification. That is what I thought. However, in the question itself, it says:

Develop a void function which takes in 2 fractions as the arguments and computes the value of a third fraction (f3) which is equal to the sum of f1 and f2. Then, print the values of f3 from int main.


Maybe the question is poorly worded, but, what I took from "print the values of f3 from int main" is that I have to write:

cout << sum(f1.numerator, f1.denominator, f2.numerator, f2.denominator);
INSIDE int main()

But I could be reading it wrongly.


If you do that, the program's output is unspecified (until C++17).


This is what I mean:

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
28
29
30
31
32
33
34
35
#include <iostream>
#include <cmath>
using namespace std;

struct fraction
{
	 int numerator;
	 int denominator;
};


void sum(int x, int y, int z, int k)
{
    double f3;
    f3 = ((double)x/y)+((double)z/k);
    cout << "sum of the two fractions = ";
    cout << f3;
}


int main()
{

fraction  f1, f2;

cout << "enter the numerator and demominator of the first  fraction: ";
cin >> f1.numerator >> f1.denominator;

cout << "enter the numerator and demominator of the second fraction: ";
cin >> f2.numerator >> f2.denominator;

sum(f1.numerator, f1.denominator, f2.numerator, f2.denominator);

return 0;
}


Which works just fine : )
Maybe the question is poorly worded

It's poorly worded. No doubt about that.

Maybe this is what is intended:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct fraction { int p, q; } f3; 

 // This poorly-designed function modifies the namespace-scope variable f3:
void sum(fraction f1, fraction f2) { 
   f3.p = (f1.p * f2.q) + (f1.q * f2.p);
   f3.q = f1.q * f2.q;

  reduce(f3);
}

int main() {
  // input f1, f2
  sum(f1, f2);
  // print f3; 
}
Thank you so much.
Topic archived. No new replies allowed.