localvars2 - Declaring local variables where they belong,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Declaring local variables where they belong, logically speaking We discuss the placement of local variable declarations. Generally, the declarations should go where they be- long, logically speaking, and this usually means placing them as close to their first use as possible. Let’s look at an example that illustrates this. Here is a procedure to sort an array segment b[0. .n] using an algorithm called selection sort . We have not discussed arrays in this course, but you have seen them in another programming language. In the first para- meter declaration, you see how to declare b to be a one-dimensional array. Just after the declaration of temp and j is a comment that states the invariant of the main loop —this is simply a picture that holds before and after each iteration of the loop, and we display the information as a pic- ture down below. The picture says that the segment b[0. .k-1] is sorted and that all of its elements are at most those in the second segment, that’s what we mean by the signs
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.
Ask a homework question - tutors are online