+ 5

Can a variable name be used for two different types?

Can x be defined by double x; and later by int x; such as in a switch? I am thinking about if you made a calculator, you would not be able to use doubles as inputs if you are going to use the % operator, so you would have to declare them as ints. Could you declare them as doubles then ints, or would (I would think) it cause an error?

26th Jan 2017, 7:46 PM
J.G.
J.G. - avatar
2 ответов
+ 1
You can not redeclare a type because when you declared int, it reserved 2 bytes in ram, but double takes 4 bytes, so you will need two more bytes, which could be already taken by other variable at that moment.
26th Jan 2017, 10:38 PM
Антон Обожин
Антон Обожин - avatar
+ 1
its called overloading name could be same but input parameters and return type must be different
7th Feb 2017, 6:39 AM
Ashish Wayne
Ashish Wayne - avatar