Monday, May 16, 2011

Use const variables as function parameters


Use const variable as function parameter whenever is possible.

Example:
foo(const char *value)
{
*value = 10; // Error, because it is a constant.
}

The reason you want to pass a const variable into a function is to guarantee the function is not able to change the value that the pointer is pointing at.

It also helps readability because by reading the pass-in parameter of the function, you knew that the const char *value will not change.