Sams Teach Yourself C++ in 21 Days

(singke) #1
The function Add()is defined on line 2. It takes two integer parameters and
returns an integer value. The program itself begins on line 8. The program
prompts the user for two numbers (line 16). The user types each number, separated by a
space, and then presses the Enter key. The numbers the user enters are placed in the vari-
ables aand bon lines 17 and 18. On line 20, the main()function passes the two num-
bers typed in by the user as arguments to the Add()function.
Processing branches to the Add()function, which starts on line 2. The values from aand
bare received as parameters firstand second, respectively. These values are printed
and then added. The result of adding the two numbers is returned on line 5, at which
point the function returns to the function that called it—main(), in this case.
On lines 17 and 18, the cinobject is used to obtain a number for the variables aand b.
Throughout the rest of the program,coutis used to write to the console. Variables and
other aspects of this program are explored in depth in the next few days.

Methods Versus Functions................................................................................


A function by any other name is still just a function. It is worth noting here that different
programming languages and different programming methodologies might refer to func-
tions using a different term. One of the more common terms used is the term method.
Method is simply another term for functions that are part of a class.

Summary ................................................................................................................


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. Today’s lesson introduced
the basic parts of a simple C++ program.

Q&A ......................................................................................................................


Q What does #includedo?
A This is a directive to the preprocessor, which runs when you call your compiler.
This specific directive causes the file in the “<>” named after the word #include
to be read in, as if it were typed in at that location in your source code.
Q What is the difference between //comments and /*style comments?
A The double-slash comments (//) “expire” at the end of the line. Slash-star (/*)
comments are in effect until a closing comment mark (*/). The double-slash com-
ments are also referred to as single-line comments, and the slash-star comments
are often referred to as multiline comments. Remember, not even the end of the

38 Day 2


ANALYSIS
Free download pdf