void return type
Sometimes we're interested in functions that return nothing at all. These
functions are called for their side effects. This is an issue
for our prototype, however, since we need to specify something for return
type. In this case, we use void in place of a return
type.
For example, look at the code on the right. The function printgreetings is only used for the side effect of writing something on the screen, not for any value it would return. |
|
bool are
traditionally referred to as predicates.
Imagine that you want to know whether an integer is a prime number or not.
We could write a function isprime with the following prototype:
bool isprime(int n);
With this predicate, you can use the function as follows:
if( isprime(n) ) // isprime returns true or false
cout << n << " is a prime number!" << endl;
Really there's no need to bring predicates up as a special
subject, since functions that return bools are not
any more special than functions that return, say,
an int. However, you might not have thought much
about the use of having such functions.
For example, suppose you wanted to define a function
max that looked at two ints and returned the
larger of the two. It's prototype would be
int max(int, int);
The definition would look like this:
int max(int a, int b)
{
if (a < b)
return b;
else
return a;
}
Note that this example shows you that you can return from
anywhere within a function (e.g., it returns b in
the middle of the function definition, if a is less than
b), just like you can return from anywhere within
main.
rep stands for repetition.
That is, it prints the char argument to the screen the
number of times given by the int.
void rep(int n, char c)
{
for(int i=0; i<n; i++)
cout << c;
cout << endl;
}
It's important to note that the order of arguments matters.
# symbol 42 times, I need to be sure to say
rep(42,'#')
int object first.
rep('#',42)
instead, what would happen?
I'd print * 35 times.
Why? Because the same kind of implicit type conversions that go on inside expressions go on with function arguments!
rep function expects an int as
its first argument, and when it gets '#' instead, it
simply converts it to an int, and the ASCII value
of '#' is 35, so that's the int you get.
char is expected as the second
argument, and when rep gets the number 42 instead, it
converts it to a character, and ASCII value 42 gives you the
char '*'.
rep("#",42)
what would happen?
The compiler would give you an errorIn particular:
rep was supposed to be an
int, but you gave it a string, and there's
no way to convert the string to an int.
max function above. How about
having three ints, x,
y and z, amongst which I need the largest. Were
I to write
max(x,y,z)
the compiler would complain ... the only max function it
knows about only takes two arguments! However, I could say the following:
max(max(x,y),z)
This is our first example of composition of functions. When the
function max gets called, its two argument expressions are
evaluated.
max(x,y), which evaluates to the
larger of the two values.
z.
max
functions taking a different number of arguments.
|
|
double f(int);
char f(int);
Answer:
No. The return type of a function is not considered in function overloading. Thus, it is illegal to have two functions whose prototype differs only in the return type. The above won't compile.
rep as follows:
| Prototypes | Definitions | main | output? (drag your mouse) |
|
|
| XXXXXX 555555555555 |
char as the first argument, so it used the first version of
rep.
int as the first argument, so it used the second version of
rep.
rep functions are quite confusing to use. Don't write
code this way.
| Definitions | Function Call | Discussion |
|
|
Error! This function call is ambiguous! The
compiler has no way of knowing:
|
rep(42.23,10); to
rep(char(42.23),10);, thus removing any
ambiguity.
Sample run:
Enter n: 10
40% of the first 10 integers are prime!
solution