+ 8

Can i declare a function in this format

public int (int x)square{ return (x*x) }

8th Mar 2017, 9:05 AM
Prabhakar Dev
Prabhakar Dev - avatar
4 Answers
+ 16
No. The parameters must be declared after method's name and you certainly want a return value: public int square(int x) { return x*x; } public: access modifier int: type of return value square: method's name (int x): parameter list of your method
8th Mar 2017, 10:10 AM
Tashi N
Tashi N - avatar
+ 15
@Very Hard Yeah, thx! Add static after access modifier if you want the method to be called without creating an object. Methods must also be static, if they are called from another static method, such as the main method.
8th Mar 2017, 10:16 AM
Tashi N
Tashi N - avatar
+ 8
@Tashi N and don't forget "static"
8th Mar 2017, 10:14 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
0
no
30th Mar 2017, 10:47 AM
ĐšĐ°ĐŒŃ€ĐŸĐœ