sqrt
and cos
that do all sorts of nice things
for us. If only there were a function getposint
that would
get a positive int from the user and return it to our program, we could
rewrite our program as:
int main()
{
int a = getposint(), b = getposint();
// Compute gcd
while(b != 0)
{
int r = a % b;
a = b;
b = r;
}
// Write out gcd
cout << "The gcd is " << a << endl;
return 0;
}
This is a tremendous improvement! Unfortunately, such a function is not a part of any of the standard libraries. Therefore, it's up to us to make it!
cmath
library that they give a description of the function
like:
double cos(double x);
This is called a prototype. It tells you (and it tells the
compiler):
// double cos ( double x );
// \______/ \___/ \________/
// return type function name input parameter
cos
is the name of the function.
cos
takes an object of type double
(that's
the x
).
In particular, it tell you that something like
cos("Hello")is not going to make sense, since
"Hello"
has type
string
, not type double
.
double
.
In particular, something like
if ( cos(3*z) && k < 0 )
isn't going to make sense, since the && operator expects an object of type
bool
and the cos(3*z)
evaluates to an object of
type double
.
cos(45)/2What happens here?
cos
takes a double object as input, the integer
45 is converted to double 45.0.
cos
results in a double object as specified
by the prototype.
main
block.
Now, in the getposint
function we'd envisioned earlier,
there's nothing that the function takes as input from the program, and it
should evaluate to or return the positive integer it's read in from the
user, so the right prototype would be:
int getposint();
The function's definition can appear anywhere after the prototype outside the main block.
return
statement in main leaves the program, a
return
statement in your function body leaves the function.
return
ing 0 however, we'll return whatever value
the function's supposed to give.
getposint
function
will have the following definition:
int getposint()
{
int k;
cout << "Enter a positive integer: ";
cin >> k;
while(k <= 0)
{
cout << "I said *positive*, try again: ";
cin >> k;
}
return k;
}
The function definition also has to appear outside of the
main
block. This program gives a
complete picture of how to rewrite our GCD calculator to make use of a
getposint
function.
int f();
int main()
{
int a, b;
a = 0;
b = f();
cout << "a = " << a << endl;
return 0;
}
int f()
{
int a = 2;
return -1;
}
What gets printed out? On the one hand, I'd say "0", since a
just got assigned that value. On the other hand, the function
f
is called in between, and there I see a
being
given the value 2. So which is it? The answer is that "0" gets printed
out.
It all goes back to scope:
a
in main
does not exist outside of
main
.
a
in the function f
does not exist
outside of f
.
Since they are in different scopes, however, there is no confusion or conflict.
We say that variables like this are local to the functions in which
they are defined, i.e. they don't exist outside of the functions in which they
are defined.
The way that you want to think of this is that each function call is like a piece of paper with boxes for each of the function's local variable.
f() in
evaluating b = f() , you should be thinking of this ...
|
pow
or sqrt
functions from
cmath
to do the factorial for us.
factorial
) takes an integer value and returns an integer value:
int factorial(int);
factorial
function. However, somewhere along the way we'll actually have to
define the function as follows:
int factorial(int x)
{
int f = 1;
while (x > 0)
{
f = f*x;
x--;
}
return f;
}
Notice that here we give a name to the int
value that gets
passed into the function, so that we can reference it within the body of
the function definition.
x
is the argument of
factorial
.
function
is called with, not the
variable itself.
So, for example, if our main
function looked like
int main()
{
int y = 4;
int z = factorial(y);
cout << y << endl;
return 0;
}
What will be the result?
Answer (drag your mouse): 4
Note the following:
y
) Note that y
is in the stack of main
.
factorial
is called, a new
stack for factorial
will be created on top of the main function.
x
) Note that x
is in the stack of factorial
.
x
and y
are different objects!
It's just that the value of y
was copied to x
when the
function was called.
x
and y
are different objects, and only
the argument x
in factorial
is modified. Therefore,
the variable y
stays the same.
y
's value (i.e. 4) got passed to
factorial
, not the variable y
itself.
void
in place of a return
type. Here's a stupid example:
void printgreetings();
int main()
{
printgreetings();
return 0;
}
void printgreetings()
{
cout << "G R E E T I N G S !" << endl << endl;
cout << "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.