By default, C++ functions pass arguments by value. This means that the formal parameters in the function definition are new variables that are initialized to the values provided by the function call. Thus, C++ functions protect the integrity of the original data by working with copies.
C++ treats an array name argument as the address of the first element of the array. Technically, this is still passing by value because the pointer is a copy of the original address, but the function uses the pointer to access the contents of the original array. When you declare formal parameters for a function (and only then), the following two declarations are equivalent:
Both of these mean that arr is a pointer to
C++ provides three ways to represent C-style strings: by using a character array, a string constant, or a pointer to a string. All are type char* (pointer-to-char), so they are passed to a function as a type char* argument. C++ uses the null character (\0) to terminate strings, and string functions test for the null character to determine the end of any string they are processing.
C++ also provides the string class to represent strings. A function can accept string objects as arguments and use a string object as a return value. The string class size() method can be used to determine the length of a stored string.
C++ treats structures the same as basic types, meaning that you can pass them by value and use them as function return types. However, if a structure is large, it might be more efficient to pass a pointer to the structure and let the function work with the original data. These same considerations apply to class objects.
A C++ function can be recursive; that is, the code for a particular function can include a call of itself.
The name of a C++ function acts as the address of the function. By using a function argument that is a pointer to a function, you can pass to a function the name of a second function that you want the first function to evoke.
Chapter Review
1. What are the three steps in using a function?
2. Construct function prototypes that match the following descriptions:
a. igor() takes no arguments and has no return value.
b. tofu() takes an int argument and returns a float.
c. mpg() takes two type double arguments and returns a double.
d. summation() takes the name of a long array and an array size as values and returns a long value.
e. doctor() takes a string argument (the string is not to be modified) and returns a double value.
f. ofcourse() takes a boss structure as an argument and returns nothing.
g. plot() takes a pointer to a map structure as an argument and returns a string.
3. Write a function that takes three arguments: the name of an int array, the array size, and an int value. Have the function set each element of the array to the int value.
4. Write a function that takes three arguments: a pointer to the first element of a range in an array, a pointer to the element following the end of a range in an array, and an int value. Have the function set each element of the array to the int value.
5. Write a function that takes a double array name and an array size as arguments and returns the largest value in that array. Note that this function shouldn’t alter the contents of the array.
6. Why don’t you use the const qualifier for function arguments that are one of the fundamental types?
7. What are the three forms a C-style string can take in a C++ program?
8. Write a function that has this prototype:
int replace(char * str, char c1, char c2);
Have the function replace every occurrence of c1 in the string str with c2, and have the function return the number of replacements it makes.
9. What does the expression *"pizza" mean? What about "taco"[2]?
10. C++ enables you to pass a structure by value, and it lets you pass the address of a structure. If glitz is a structure variable, how would you pass it by value? How would you pass its address? What are the trade-offs of the two approaches?
11. The function judge() has a type int return value. As an argument, it takes the address of a function. The function whose address is passed, in turn, takes a pointer to a const char as an argument and returns an int. Write the function prototype.
12. Suppose we have the following structure declaration:
struct applicant {
char name[30];
int credit_ratings[3];
};