CONSTANT ARGUMENTS

When arguments are passed by value, the data in calling code is not affected by a change in functions. Although some data (like arrays) can’t be passed by value but should be passed by pointers and references, which risks the possible change during the function call. Programmer which will write the function will surely pay closer attention to not changing the value of the arguments only if it’s necessary. What’s most likely to happen later on is that he forget the rule while correcting a function or someone else take radical procedure and change the body of the function.
To avoid trouble which can be caused by subsequent careless change of code it is recommended that arguments which are passed by pointers and references, and which we want to change stay constant.
Let’s illustrate that on a example of a function Lengthofarray() which will serve to calculate the length of string. Argument string will become the pointer on unchanged string if we add in the declaration of function modification const before the type of the argument (in this case char).


Share on Google Plus