Why colons and not equals? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why colons and not equals?

In the expression int res = Area(w: 5, h: 8); , why colon and not equal symbol?

19th May 2017, 6:44 PM
Marcelo Miney Mendes
Marcelo Miney Mendes - avatar
2 Answers
+ 4
We would need a reference to w and h to be able to use the equals sign here. Since that would be assigning h and w to the value of whatever is on the opposite side of the equals sign. We cannot assign them without a reference. So instead they use a colon to indicate which variable you are referring to when calling the function. It is not mandatory, you could just write Area(5,8); Although if you don't write which variable you are referring to, the order matters. If I did Area(8,5); 8 would be the width, assuming the function was written like this: int Area(int w, int h){}
19th May 2017, 7:02 PM
Rrestoring faith
Rrestoring faith - avatar
0
this called named parameters, and that's it's syntax. you can read more here: https://www.dotnetperls.com/named-parameters
19th May 2017, 11:20 PM
Ahmed Albarakany
Ahmed Albarakany - avatar