What's the convention for naming functions in C++?
I come from the Java environment so I usually name something like:
myFunction(...) { }
I've seen mixed code in C++,
myFunction(....) MyFunction(....) Myfunction(....)
What's the correct way?
Also, is it the same for a class method as well as a non-class method?
All methods and functions should begin with a capital letter. The first letter of each word should also be capitalized. Special characters such as dashes and underscores are prohibited.
In programming languagesSome programming languages are case-sensitive for their identifiers (C, C++, Java, C#, Verilog, Ruby, Python and Swift).
According to the book "Javascript: the good parts", you should only capitalise the first character of the name of a function when you need to construct the object by "new" keyword.
Function names should be lowercase, with words separated by underscores as necessary to improve readability. Variable names follow the same convention as function names. mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.
Since C++11, you may want to use either snake_case
or camelCase
for function names.
This is because to make a class work as the range-expression in a range-based for-loop, you have to define functions called begin
and end
(case-sensitive) for that class.
Consequently, using e.g. PascalCase
for function names means you have to break the naming consistency in your project if you ever need to make a class work with the range-based for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With