Why is the output 16 and not 4?

#include <iostream>
#include <cmath>
using namespace std;
int multiplication(int& a);
int addition(int& b);
int square_root(int& c);
int printing(int& d);
int main()
{
int x = 4;
multiplication(x);
addition(x);
square_root(x);
printing(x);
return 0;
}
int multiplication(int& a)
{
a = a * 3;
return 0;
}
int addition(int& b)
{
b = b + 4;
return 0;
}
int square_root(int& c)
{
sqrt(c);
return 0;
}
int printing(int& d)
{
cout << d;
return 0;
}
You didn't assign the value of sqrt(c) to the variable c in your square root function.

Incidentally, if the functions are doing one thing to one variable then it is more obvious to return that as the value of the function, not through an argument passed as a reference.
Yep! This is way better! Thank you!
#include <iostream>
#include <cmath>
using namespace std;

int multiplication(int a)
{
a = a * 3;
return a;
}
int addition(int b)
{
b = b + 4;
return b;
}
int square_root(int c)
{
c = sqrt(c);
return c;
}
int printing(int d)
{
cout << d;
return d;
}
int main()
{
int x = 4;
multiplication(x);
addition(x);
square_root(x);
printing(x);
return 0;
}
Incidentally, if the functions are doing one thing to one variable then it is more obvious to return that as the value of the function, not through an argument passed as a reference.

Agreed. Also, giving the function a return type of int, but always returning a value of zero is at the very least a wasted effort, but it is also misleading, as a user of the function would reasonably expect that the value being returned had some significance.
Topic archived. No new replies allowed.