2

I'm quite new to learning the C++ language. Tried searching, but maybe I just don't understand the terminology correctly enough to know what to properly search for.

I'm learning about user defined functions, and was wondering what were the conventions for naming arguments/variables that are created in new functions. I know the code isn't perfect but I'm shortening it for conciseness.

int main()
{
    int hours;
    cout << "Enter hours: ";
    cin >> hours;

    int minutes;
    cout << "Enter minutes: ";
    cin >> minutes;

    displayTime(hours, minutes);

    return 0;
}

So now I will create, what I consider in my novice mind, dummy variables in the function being passed arguments to. What are the conventions for these names? Here are two examples to show possible names.

void displayTime(int a, int b)     // new names totally different
    cout << a << ":" << b << "\n";
}

or

void displayTime(int hours, int minutes)     // same variable names
    cout << hours << ":" << minutes << "\n";
}

Is there another, most accepted/common, option? I just want to make sure I start good habits with fundamental concepts. I'm using the C++ Primer Plus 6th edition, for what it's worth.

  • 6
    Things should be named after what they do or what they are. Never be cryptic and don't be afraid to `useVeryLongButDescriptiveNamesLikeThis`. This isn't the 1970s where memory was rare, and we have IDEs now with runtime intelligence of the code, so please avoid the use of (bad) Hungarian notation, like naming variables `lpszSomething`. – Dai Oct 25 '15 at 07:40
  • 1
    Dustin, as you continue to learn C++, it's well worth considering using different types for different things. Instead of two int types with meaning specified by the parameter names, you would have an hour _type_ and a minutes _type_. Have a look at this example by Scot Meyers. It is also also found in the book effective C++ as "Make interfaces easy to use correctly and hard to use incorrectly.". http://www.aristeia.com/Papers/IEEE_Software_JulAug_2004_revised.htm – Johan Lundberg Oct 25 '15 at 08:10

0 Answers0