⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02.htm

📁 vc的电子书
💻 HTM
📖 第 1 页 / 共 2 页
字号:
	<LI>The author's name.
	<P>
	<LI>A revision history (notes on each change made).
	<P>
	<LI>What compilers, linkers, and other tools were used to make the program.
	<P>
	<LI>Additional notes as needed.
</UL>

<P>For example, the following block of comments might appear at the top of the Hello
World program.</P>
<PRE><FONT COLOR="#0066FF">/************************************************************

Program:      Hello World

File:         Hello.cpp

Function:     Main (complete program listing in this file)

Description:  Prints the words &quot;Hello world&quot; to the screen

Author:       Jesse Liberty (jl)

Environment:  Turbo C++ version 4, 486/66 32mb RAM, Windows 3.1
              DOS 6.0.  EasyWin module.

Notes:        This is an introductory, sample program.

Revisions:    1.00  10/1/94 (jl) First release
              1.01  10/2/94 (jl) Capitalized &quot;World&quot;

************************************************************/
</FONT></PRE>
<P>It is very important that you keep the notes and descriptions up-to-date. A common
problem with headers like this is that they are neglected after their initial creation,
and over time they become increasingly misleading. When properly maintained, however,
they can be an invaluable guide to the overall program.</P>
<P>The listings in the rest of this book will leave off the headings in an attempt
to save room. That does not diminish their importance, however, so they will appear
in the programs provided at the end of each week.
<H4 ALIGN="CENTER"><A NAME="Heading15"></A><FONT COLOR="#000077">A Final Word of
Caution About Comments</FONT></H4>
<P>Comments that state the obvious are less than useful. In fact, they can be counterproductive,
because the code may change and the programmer may neglect to update the comment.
What is obvious to one person may be obscure to another, however, so judgment is
required.</P>
<P>The bottom line is that comments should not say what is happening, they should
say why it is happening.


<BLOCKQUOTE>
	<P>
<HR>
<B>DO</B> add comments to your code. <B>DO</B> keep comments up-to-date. <B>DO</B>
	use comments to tell what a section of code does.<B> DON'T</B> use comments for self-explanatory
	code. 
<HR>


</BLOCKQUOTE>

<H3 ALIGN="CENTER"><A NAME="Heading16"></A><FONT COLOR="#000077">Functions</FONT></H3>
<P>While main() is a function, it is an unusual one. Typical functions are called,
or invoked, during the course of your program. A program is executed line by line
in the order it appears in your source code, until a function is reached. Then the
program branches off to execute the function. When the function finishes, it returns
control to the line of code immediately following the call to the function.</P>
<P>A good analogy for this is sharpening your pencil. If you are drawing a picture,
and your pencil breaks, you might stop drawing, go sharpen the pencil, and then return
to what you were doing. When a program needs a service performed, it can call a function
to perform the service and then pick up where it left off when the function is finished
running. Listing 2.4 demonstrates this idea.</P>

<P><A NAME="Heading17"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 2.4. Demonstrating
a call to a function.</B></FONT><FONT SIZE="2" COLOR="#000077"><B></B></FONT>
<PRE><FONT COLOR="#0066FF">1:     #include &lt;iostream.h&gt;
2:
3:     // function Demonstration Function
4:     // prints out a useful message
5:     void DemonstrationFunction()
6:     {
7:         cout &lt;&lt; &quot;In Demonstration Function\n&quot;;
8:     }
9:
10:    // function main - prints out a message, then
11:    // calls DemonstrationFunction, then prints out
12:    // a second message.
13:    int main()
14:    {
15:        cout &lt;&lt; &quot;In main\n&quot; ;
16:        DemonstrationFunction();
17:        cout &lt;&lt; &quot;Back in main\n&quot;;
18:         return 0;
19: }
In main
In Demonstration Function
Back in main
</FONT></PRE>
<P>The function DemonstrationFunction() is defined on lines 5-7. When it is called,
it prints a message to the screen and then returns.</P>
<P>Line 13 is the beginning of the actual program. On line 15, main() prints out
a message saying it is in main(). After printing the message, line 16 calls DemonstrationFunction().
This call causes the commands in DemonstrationFunction() to execute. In this case,
the entire function consists of the code on line 7, which prints another message.
When DemonstrationFunction() completes (line 8), it returns back to where it was
called from. In this case the program returns to line 17, where main() prints its
final line.
<H4 ALIGN="CENTER"><A NAME="Heading19"></A><FONT COLOR="#000077">Using Functions</FONT></H4>
<P>Functions either return a value or they return void, meaning they return nothing.
A function that adds two integers might return the sum, and thus would be defined
to return an integer value. A function that just prints a message has nothing to
return and would be declared to return void.</P>
<P>Functions consist of a header and a body. The header consists, in turn, of the
return type, the function name, and the parameters to that function. The parameters
to a function allow values to be passed into the function. Thus, if the function
were to add two numbers, the numbers would be the parameters to the function. Here's
a typical function header:</P>
<PRE><FONT COLOR="#0066FF">int Sum(int a, int b)
</FONT></PRE>
<P>A parameter is a declaration of what type of value will be passed in; the actual
value passed in by the calling function is called the argument. Many programmers
use these two terms, parameters and arguments, as synonyms. Others are careful about
the technical distinction. This book will use the terms interchangeably.</P>
<P>The body of a function consists of an opening brace, zero or more statements,
and a closing brace. The statements constitute the work of the function. A function
may return a value, using a return statement. This statement will also cause the
function to exit. If you don't put a return statement into your function, it will
automatically return void at the end of the function. The value returned must be
of the type declared in the function header.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT> Functions are covered in more detail on
	Day 5, &quot;Functions.&quot; The types that can be returned from a function are
	covered in more det+[radical][Delta][infinity]on Day 3. The information provided
	today is to present you with an overview, because functions will be used in almost
	all of your C++ programs. 
<HR>


</BLOCKQUOTE>

<P>Listing 2.5 demonstrates a function that takes two integer parameters and returns
an integer value. Don't worry about the syntax or the specifics of how to work with
integer values (for example, int x) for now; that is covered in detail on Day 3.</P>

<P><A NAME="Heading20"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 2.5. FUNC.CPP
demonstrates a simple function.</B></FONT><FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">1:    #include &lt;iostream.h&gt;
2:    int Add (int x, int y)
3:    {
4:
5:      cout &lt;&lt; &quot;In Add(), received &quot; &lt;&lt; x &lt;&lt; &quot; and &quot; &lt;&lt; y &lt;&lt; &quot;\n&quot;;
6:      return (x+y);
7:    }
8:
9:    int main()
10:    {
11:         cout &lt;&lt; &quot;I'm in main()!\n&quot;;
12:         int a, b, c;
13:         cout &lt;&lt; &quot;Enter two numbers: &quot;;
14:         cin &gt;&gt; a;
15:         cin &gt;&gt; b;
16:         cout &lt;&lt; &quot;\nCalling Add()\n&quot;;
17:         c=Add(a,b);
18:         cout &lt;&lt; &quot;\nBack in main().\n&quot;;
19:         cout &lt;&lt; &quot;c was set to &quot; &lt;&lt; c;
20:         cout &lt;&lt; &quot;\nExiting...\n\n&quot;;
21:         return 0;
22: }

I'm in main()!
Enter two numbers: 3 5

Calling Add()
In Add(), received 3 and 5

Back in main().
c was set to 8

Exiting...
</FONT></PRE>
<P>The function Add() is defined on line 2. It takes two integer parameters and returns
an integer value. The program itself begins on line 9 and on line 11, where it prints
a message. The program prompts the user for two numbers (lines 13 to 15). The user
types each number, separated by a space, and then presses the Enter key. main() passes
the two numbers typed in by the user as arguments to the Add() function on line 17.</P>
<P>Processing branches to the Add() function, which starts on line 2. The parameters
a and b are printed and then added together. The result is returned on line 6, and
the function returns.</P>
<P>In lines 14 and 15, the cin object is used to obtain a number for the variables
a and b, and cout is used to write the values to the screen. Variables and other
aspects of this program are explored in depth in the next few days.
<H3 ALIGN="CENTER"><A NAME="Heading22"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>The difficulty in learning a complex subject, such as programming, is that so
much of what you learn depends on everything else there is to learn. This chapter
introduced the basic <BR>
parts of a simple C++ program. It also introduced the development cycle and a number
of important new terms.
<H3 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">Q&amp;A</FONT></H3>

<DL>
	<DD><B>Q. What does #include do?</B><BR>
	<BR>
	<B>A.</B> This is a directive to the preprocessor, which runs when you call your
	compiler. This specific directive causes the file named after the word include to
	be read in, as if it were typed in at that location in your source code.<BR>
	<BR>
	<B>Q. What is the difference between // comments and /* style comments?<BR>
	</B><BR>
	<B>A.</B> The double-slash comments (//) &quot;expire&quot; at the end of the line.
	Slash-star (/*) comments are in effect until a closing comment (*/). Remember, not
	even the end of the function terminates a slash-star comment; you must put in the
	closing comment mark, or you will get a compile-time error.<BR>
	<BR>
	<B>Q. What differentiates a good comment from a bad comment?<BR>
	</B><BR>
	<B>A. </B>A good comment tells the reader why this particular code is doing whatever
	it is doing or explains what a section of code is about to do. A bad comment restates
	what a particular line of code is doing. Lines of code should be written so that
	they speak for themselves. Reading the line of code should tell you what it is doing
	without needing a comment.
</DL>

<H3 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">Workshop</FONT></H3>
<P>The Workshop provides quiz questions to help you solidify your understanding of
the material covered and exercises to provide you with experience in using what you've
learned. Try to answer the quiz and exercise questions before checking the answers
in Appendix D, and make sure you understand the answers before continuing to the
next chapter.
<H4 ALIGN="CENTER"><A NAME="Heading25"></A><FONT COLOR="#000077">Quiz</FONT></H4>

<DL>
	<DD><B>1.</B> What is the difference between the compiler and the preprocessor?<BR>
	<B><BR>
	2.</B> Why is the function main() special?<BR>
	<B><BR>
	3.</B> What are the two types of comments, and how do they differ?<BR>
	<B><BR>
	4</B>. Can comments be nested?<BR>
	<B><BR>
	5.</B> Can comments be longer than one line?
</DL>

<H4 ALIGN="CENTER"><A NAME="Heading26"></A><FONT COLOR="#000077">Exercises</FONT></H4>

<DL>
	<DD><B>1.</B> Write a program that writes I love C++ to the screen.<BR>
	<B><BR>
	2. </B>Write the smallest program that can be compiled, linked, and run.<BR>
	<B><BR>
	3.</B> BUG BUSTERS: Enter this program and compile it. Why does it fail? How can
	you fix it?
</DL>

<PRE><FONT COLOR="#0066FF">1: #include &lt;iostream.h&gt;
2: void main()
3: {
4: cout &lt;&lt; Is there a bug here?&quot;;
5: }
</FONT></PRE>

<DL>
	<DD><B>4.</B> Fix the bug in Exercise 3 and recompile, link, and run it.
	<CENTER>
	<DD><BR>
	<BR>
	<BR>
	<A HREF="ch01.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch01.htm"><IMG SRC="BLANPREV.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANPREV.GIF" WIDTH="37"
	HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="tppmsgs/msgs0.htm#1" tppabs="http://www.mcp.com/sams"><IMG
	SRC="BLANHOME.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"
	BORDER="0"></A><A HREF="index.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/index.htm"><IMG SRC="BLANTOC.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANTOC.GIF"
	WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch03.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch03.htm"><IMG SRC="BLANNEXT.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANNEXT.GIF"
	WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="#heading1"><IMG SRC="BLANTOP.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANTOP.GIF"
	WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A></CENTER>
</DL>



</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -