Module 2, part 1. More on methods
Introduction
You have seen functions or procedures in other languages, so we do not go into an elaborate discussion here. For concepts and ideas that are similar to those in many other languages, we provide brief summaries on html pages and point you to further readings in the course text. For new material, we provide more complete explanations in web lectures.
Contents
No. | Topic | Discussion |
---|---|---|
1. | The importance of method specifications. (blecture doc ppt.pdf) Reading: Gries/Gries, Sec. 13.3.1, p. 376–378. |
We discuss method specifications and the standards and conventions we use in this course. |
2. | Method headers. (html) |
We summarize method headers and show the difference in a header for a function, a procedure, and a constructor. |
3. | Function and procedure calls. (html) | We show what calls look like. |
Reading for items 2 and 3: | Gries/Gries, Sec. 2.1—2.3.3, pp. 45–62. | |
4. | Local variables. (blecture doc ppt.pdf) |
A local variable is a variable that is declared in the body of a method. We discuss its use and its scope. |
5. | Put a local-variable declaration where it belongs. (blecture doc ppt.pdf) |
The tendency is to put local variable declarations at the beginning of the method body. Don't do it. Instead, put the declarations where they belong, logically speaking, which is generally close to their first use. |
Reading for items 4 and 5: | Gries/Gries, Sec. 2.3.7, pp. 76–78; p. 371; p. 383–384. | |
6. | The block, conditional statement, and loop. (html) Reading: Gries/Gries Sec. 2.3.3, pp. 68–71; Sec. 2.3.8, p.p 78-81; Chap. 7, pp 233–265. |
Conditional statements are essentially the same in all languages.
Here, we briefly summarize their syntax and execution. Gries/Gries Chap. 7 contains a thorough study of while loops and for loops. You will not be tested on this material, but you may have to use loops in the asssignments. Use this chapter as a reference. |
7. | Arrays. (html) Reading: Gries/Gries, Chaps. 8 and 9. |
We briefly summarize one-dimensional arrays. This is for your convenience
only; you do not have to read this page. Arrays may be used as
a matter of course in an assignment, but you do not have to know
about them for a test. Gries/Gries Chaps. 8 and 9 contains a thorough study of one-dimensional and multi-dimensional arrays. You will not be tested on this material, but you may have to use arrays in the asssignments. Use these chapters as a reference. |
8. | Return-statements. (html) Reading: Gries/Gries Secs. 2.3.5–2.3.6, pp. 7476. |
Execution of a method can be terminated using a return statement. The return-statement for a function includes an expression, whose value is returned; the return-statement in a procedure or constructor does not. |
9. | Executing method calls. (blecture doc ppt.pdf) Reading: Gries/Gries, Sec. 2.7, pp. 93–97. |
We show in detail how a method call is executed, including drawing
a frame for the call and erasing it when the call is finished.
This material is important because it allows you to answer various
questions about methods, such as how long a local variable "lives"
and when it is created as well as how the parameter-argument correspondence
is managed. Sec 2.7 of Gries/Gries talks about a "call stack", which contains all the frames for calls still being executed. In this blecture, we abstract away and don't mention the call stack; we just draw the frame for a call anywhere we want. |