Simple argument declaration in function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Simple argument declaration in function

Why there is not in c++ possible to create a function with simple argument declaration like (int x, y, z, a)? Instead of (int x, int y, int z, int a)? // example function int addNumbers(int x, y, z, a) { ... } I believed you can simplify the arguments in that manner in other languages. Isn't that correct?

17th Jan 2018, 3:25 PM
Miroslav M
Miroslav M - avatar
3 Answers
+ 3
i tried to declare parameters like this in java but it always wants a data type but it works fine outside a method mabe python would do declarations like this.
17th Jan 2018, 5:46 PM
D_Stark
D_Stark - avatar
+ 2
In C, you can actually omit the type of a parameter and it's assumed to be int. int addNumbers(a, b, c){ ... } // three ints As far as I'm aware they copied that behaviour over to C++. Eventually removed it, because it just leads to bugs and choosing int as a default is not a better choice than any other type, so why should it be int. So basically your idea is not a thing in C++ because even if they wanted implement that they couldn't, since not writing a type was already taken up by another feature. They could add it as a feature now, but I guess there isn't really a big usecase and it's not a massive timesaver. Chances are that if all your functions take 4 ints, you should pack the 4 ints into a class, like struct Point{ int x,y,z,a; } addNumbers(Point& p){ ... } to make your code even less repetitive. I'm not aware of any language that does it they way you describe, it's an interesting idea though! EDIT: Maybe I'm also wrong and it's the best feature on earth, it's just that nobody has ever thought of it. You can always write a proposal to the C++ committee, at https://isocpp.org/std/submit-a-proposal ! C++ is always evolving. I wouldn't count on your feature being added to the language though.
17th Jan 2018, 4:10 PM
Schindlabua
Schindlabua - avatar
+ 1
@Schindlabua: Thank you. Also thanks for the nice words :) but I do not think any one ever thought of that. There should be a reason. I think it is in this manner used in VBA or Pascal.
17th Jan 2018, 4:48 PM
Miroslav M
Miroslav M - avatar