Use const variables as function parameters when po

This tip submitted by beyonddc on 2006-01-28 23:47:39. It has been viewed 19046 times.
Rating of 5.2 with 155 votes



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.



More tips

Help your fellow programmers! Add a tip!