Chapter 6 explained that when an argument is passed by value, a copy of the argument in the function call is made and passed to the function. If the copy is modified in the func- tion, the original value in the caller does not change. In many cases, a value passed to a func- tion is modified in that function. However, in some instances, the value should not be altered in the called function, even though the called function manipulates only a copy of the original value.Consider a function that takes a one-dimensional array and its size as arguments and subsequently prints the array. Such a function should loop through the array and output each element individually. The size of the array is used in the function body to determine the array’s highest subscript so the loop can terminate when the printing completes. The array’s size does not need to change in the function body, so it should be declared const to ensure that it will not change. Because the array is only being printed, it, too, should be declared const. This is especially important because arrays are always passed by reference and could easily be changed in the called function. If an attempt is made to modify a const value, an error occurs.
đang được dịch, vui lòng đợi..