void in place of a return
type. Here's an example:
void printgreetings(); // void return type: a function that returns nothing
int main()
{
printgreetings();
return 0;
}
void printgreetings()
{
cout << "G R E E T I N G S !" << endl
<< endl
<< "This is a little demo program." << endl
<< "I hope you enjoy it." << endl;
}
This function 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 to do this.
The prototype would be as follows:
bool isprime(int n);
Note that specifying a name for the parameter is actually not required in the
prototype (only in the function definition), so we could simplify the prototype to be:
bool isprime(int);
We can define the function as follows:
bool isprime(int n)
{
if (i <= 1)
return false;
for (int i=2; i < n; i++)
{
if (n % i == 0 )
return false;
}
return true;
}
With the definition of 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,
strings. 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.
max defined as below, using
the conditional (or ternary) operator ?:
int max(int a, int b)
{
return (b > a) ? b : a;
}
but that my program had 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. The first is max(x,y), which evaluates to the
larger of the two values, and the second is simply z. So,
what we get out of this is the maximum of all three values.
The most important thing I can tell you about composing functions, is that there is really nothing to talk about. Function arguments are given in your code by expressions, right? And those expressions are evaluated before calling the function to produce the argument objects that are passed to the function. So, whether or not the argument expressions themselves contain function calls is immaterial — the system works the same.
void rep(int,char);
The function function name rep stands for repetition.
That is, it prints the char argument to the screen the
number of times given by the int.
Order of arguments.
It's important to note that the order of arguments matters. For
example,
If I want to print a # symbol 42 times, I need to be sure
to say rep(42,'#'), because the function expects the
int object first.
rep('#',42) instead, do you know what'd happen?
A: 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 '*'.
Arguments' data type. When implicit conversions aren't possible, the compiler gives you an error message.
rep("#",42), what would
happen?
A: The compiler would give you an error saying:
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.
rep, but with different types of arguments?
For example, what if I have the following program
| Prototypes | Definitions | main | output |
|
|
| XXXXXX 555555555555 |
char as the first argument, so it used the version of
rep with an object of type char as a first argument.
int as the first argument, so it used the version of
rep with an object of type int as a first argument.
rep, the name of the
function is really rep(int,int) or
rep(char,int).
Q: Will the following code compile?
double f(int);
char f(int);
A:
Note 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.
max
functions taking a different number of arguments.
|
|
| Definitions | Function Call | Discussion |
|
|
Error! This function call is ambiguous! The
compiler has no way of knowing:
|
Fixing the issues: When ambiguities like this arise, simply use explicit
conversion to disambiguate. For the previous example, I might
change the call rep(42.23,10); to
rep(char(42.23),10);, thus removing any
ambiguity.