Previous Table of Contents Next


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.

The name of the function and its parameters (that is the header without the return value) is called the function’s signature.

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.

Listing 2.4 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 will be covered soon.

Listing 2.4 FUNC.CPP Demonstrates a Simple Function


1:    #include <iostream.h>
2:    int Add (int x, int y)
3:    {
4:
5:       cout << “In Add(), received ” << x << “ and ” << y << “\n”;
6:       return (x+y);
7:    }
8:
9:    int main()
10:   {
11:      cout << “I’m in main()!\n”;
12:      int a, b, c;
13:      cout << “Enter two numbers: ”;
14:      cin >> a;
15:      cin >> b;
16:      cout << “\nCalling Add()\n”;
17:      c=Add(a,b);
18:      cout << “\nBack in main().\n”;
19:      cout << “c was set to “ << c;
20:      cout << “\nExiting...\n\n”;
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...


The function Add() is defined on line 2. It takes two integer parameters and returns an integer value. The program itself begins 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 Enter. Main() passes the two numbers typed in by the user as arguments to the Add() function on line 17.

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.

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 will be explored in depth in the next few days.

Summary

In this hour you examined a program in some detail. You learned how to include files using #include, and you learned how to use comments well. You also learned what a function is and how it is used in a program.

Q & A

Q What does #include do?
A 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.
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 (*/). 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.
Q What differentiates a good comment from a bad comment?
A 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.


Previous Table of Contents Next