What would a return type be?

Write your question here.

Hello I have just started coding in c++, I have a question regarding returning datatypes. Suppose you have input 4 variables of the following nature

int a = 15;
double b = 30949.374;
float c = 230.47;
long d = 1000000;

and z = (a + d) * (c + b);

what would the return type of z be?

I know we can use either int, double, float and long to set the datatype of z, but what I want to know what would the resultant datatype be if all these 4 numbers are multiplied.

Thank you.
Consider this:

1
2
double x = 1.5;
int y = 3;


3 * 1.5 = 4.5;

You always need "z" to be able to hold decimals when it is not integer multiplication. So make it a double/float.

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

int main()
{
	int a = 15;
	double b = 30949.374;
	float c = 230.47;
	long d = 1000000;

	auto z = (a + d) * (c + b); // Google the auto keyword c++ to find out what it does

	std::cout << z << std::endl;
	return 0;
}
Last edited on
So in this case it should be a double then. I.e to be on the safer side.
And thank you for the explanation.
So in this case it should be a double then. I.e to be on the safer side.

Yes.

I edited my post with an example in which I used the auto key word -
http://en.cppreference.com/w/cpp/language/auto
Topic archived. No new replies allowed.