{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


02-Getting-Started - CS106X Winter 2008 Handout 02 January...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 02 Winter 2008 January 9, 2008 Early Programming: Landmarks and Landmines Handout written by Julie Zelenski, Mehran Sahami, and Robert Plummer. After today’s lecture, you should run home read all of Chapter 1 on your own. We won't teach the basic syntax and constructs. We’ll just highlight some of the common programming idioms and C++ idiosyncrasies. This handout contains a smattering of facts about the C++ language, mentions some of the basics, and points out a few things to avoid. Most programs have the same component sections —A comment explaining what the program does This is a good programming practice that makes your code more accessible to the reader (who might be you!) —Library inclusions These #include statements let the compiler know what libraries you want to use functions from. Many standard libraries come with all C++ compilers. You will also use libraries you write yourself, or in this course, libraries that we provide such as genlib or simpio . —Constant definitions By using the #define mechanism to define constants, you make your program more readable and avoid "magic numbers". You also make it easy to change the value of the constant. —Function prototypes One common style is to write the code for functions after the main program. This means that functions are often called before the code appears. Function prototypes let the compiler compile the calls correctly and make sure they are consistent with the function code. —Main program The main program is actually a function called main . A prototype for this function is not required. Execution of your program begins with the first line of the main function. The function main takes no arguments and returns an int (as shown below); typically we return 0 to indicate the program exited cleanly —Function definitions All programs worth writing involve breaking the code into functions, so that no part of the program manages too large a detail. Good decomposition leads to code that is clear, logical, and easy to understand. The first assignment, in fact, is an exercise in decomposition, and the A program will make it a point to write functions that can be called from two or more different places.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Variables must be declared before they’re used Variables are local to the function in which they are declared. We will never use global variable, and you won’t either. In C++, local variables can be declared anywhere with a block of statements. The scope of a local variable extends to the end of the enclosing block.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}