📄 tut2-3.html
字号:
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << divide (x,y);
cout << "\n";
cout << divide (n,m);
cout << "\n";
return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top">
<B><TT>2<BR>2.5</TT></B>
</TD></TR></TABLE>
</CENTER>
<P>
In this case we have defined two functions with the same name, but one of them accepts
two arguments of type <tt><b>int</b></tt> and the other accepts them of type
<tt><b>float</b></tt>. The compiler knows which one to call in each case by examining the
types when the function is called. If it is called with two <tt><b>int</b></tt>s as
arguments it calls to the function that has two <tt><b>int</b></tt> arguments in the
prototype and if it is called with two <tt><b>float</b></tt>s it will call to the one
which has two <tt><b>float</b></tt>s in its prototype.
<p>
For simplicity I have included the same code within both functions, but this is not
compulsory. You can make two function with the same name but with completely
different behaviors.
<p>
<h2><i>inline</i> functions.</h2>
The <i>inline</i> directive can be included before a function declaration to specify
that the function must be compiled as code at the same point where it is called. This is
equivalent to declaring a macro. Its advantage is only appreciated in very short
functions, in which the resulting code from compiling the program may be faster
if the overhead of calling a function (stacking of arguments) is avoided.
<p>
The format for its declaration is:<br>
<tt><b>inline <i>type name</i> ( <i>arguments ...</i> ) { <i>instructions ...</i> }</b></tt><br>
and the call is just like the call to any other function. It is not necessary to
include the <tt><i>inline</i></tt> keyword before each call, only in the declaration.
<p>
<h2>Recursivity.</h2>
Recursivity is the property that functions have to be called by themselves. It is useful
for tasks such as some sorting methods or to calculate the factorial of
a number. For example, to obtain the factorial of a number (n) its mathematical formula
is:<br>
<blockquote>
<tt><i>n</i>! = <i>n</i> * (<i>n</i>-1) * (<i>n</i>-2) * (<i>n</i>-3) ... * 1</tt><br>
</blockquote>
more concretely, 5! (factorial of 5) would be:<br>
<blockquote><tt>5! = 5 * 4 * 3 * 2 * 1 = 120</tt><br></blockquote>
and a recursive function to do that could be this:<br>
<P>
<CENTER>
<TABLE WIDTH=100% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// factorial calculator</I>
#include <iostream.h>
long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}
int main ()
{
long l;
cout << "Type a number: ";
cin >> l;
cout << "!" << l << " = " << factorial (l);
return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top">
<TT><B>Type a number:</B> 9<BR>
<B>!9 = 362880</B></TT>
</TD></TR></TABLE>
</CENTER>
<P>
Notice how in function <tt><b>factorial</b></tt> we included a call to itself,
but only if the argument is greater than <tt><b>1</b></tt>,
since otherwise the function would perform
<i>an infinite recursive loop</i> in which once it arrived at <tt><b>0</b></tt>
it would continue multiplying by all the negative numbers (probably provoking a
stack overflow error on runtime).
<P>
This function has a limitation because of the <i>data type</i> used in its design (<tt>long</tt>)
for more simplicity. In a standard system, the type <TT><B>long</B></TT>
would not allow storing factorials greater than <tt><b>12!</b></tt>.
<p>
<h2>Prototyping functions.</h2>
Until now, we have defined the all of the functions before the
first appearance of calls to them, that generally was in
<tt><b>main</b></tt>, leaving the function <tt><b>main</b></tt> for the end.
If you try to repeat some of the examples of functions described so far, but placing
the function <tt><b>main</b></tt> before any other function that is called from within
it, you will most likely obtain an error. The reason is that to be able to call a
function it must have been declared previously (it must be known), like we have done
in all our examples.
<p>
But there is an alternative way to avoid writing all the code of all functions before
they can be used in <tt><b>main</b></tt> or in another function. It is by <i>prototyping
functions</i>. This consists in making a previous shorter, but quite significant,
declaration of the complete definition so that the compiler can know the arguments
and the return type needed.
<p>
Its form is:
<blockquote>
<tt><b><i>type name</i> ( <i>argument_type1</i>, <i>argument_type2</i>, ...);</b></tt><br>
</blockquote>
It is identical to the header of a function definition, except:
<ul>
<li>It does not include a <tt><i>statement</i></tt> for the function.
That means that it does not include the body with all the instructions that are usually
enclose within curly brackets <tt>{ }</tt>.
<li>It ends with a semicolon sign (<tt>;</tt>).
<li>In the argument enumeration it is enough to put the type of each argument.
The inclusion of a name for each argument as in the definition of a standard function
is optional, although recommended.
</ul>
For example:
<P>
<CENTER>
<TABLE WIDTH=100% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// prototyping</I>
#include <iostream.h>
void odd (int a);
void even (int a);
int main ()
{
int i;
do {
cout << "Type a number: (0 to exit)";
cin >> i;
odd (i);
} while (i!=0);
return 0;
}
void odd (int a)
{
if ((a%2)!=0) cout << "Number is odd.\n";
else even (a);
}
void even (int a)
{
if ((a%2)==0) cout << "Number is even.\n";
else odd (a);
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top">
<TT><B>Type a number (0 to exit):</B> 9<BR>
<B>Number is odd.</B><BR>
<B>Type a number (0 to exit):</B> 6<BR>
<B>Number is even.</B><BR>
<B>Type a number (0 to exit):</B> 1030<BR>
<B>Number is even.</B><BR>
<B>Type a number (0 to exit):</B> 0<BR>
<B>Number is even.</B><BR>
</TT>
</TD></TR></TABLE>
</CENTER>
<P>
This example is indeed not an example of effectiveness, I am sure that at this point
you can already make a program with the same result using only half of the code lines.
But this example ilustrates how protyping works. Moreover, in this concrete case the prototyping
of -at least- one of the two functions is necessary.
<p>
The first things that we see are the prototypes of functions <tt><b>odd</b></tt> and
<tt><b>even</b></tt>:
<blockquote><tt>
void odd (int a);<br>
void even (int a);
</tt></blockquote>
that allows these functions to be used before they are completely defined, for example,
in <tt><b>main</b></tt>, which now is located in a more logical place: the beginning of
the program's code.
<P>
Nevertheless, the specific reason why this program needs at
least one of the functions prototyped is because in <tt><b>odd</b></tt> there is
a call to <tt><b>even</b></tt> and in <tt><b>even</b></tt> there is a call to
<tt><b>odd</b></tt>. If none of the two functions had been previously
declared, an error would have happened, since either <tt><b>odd</b></tt>
would not be visible from <tt><b>even</b></tt> (because it has not still been declared),
or <tt><b>even</b></tt> would not be visible from <tt><b>odd</b></tt>.
<p>
Many programmers recommend that <U>all</U> functions be prototyped.
It is also my recommendation, mainly in case that there are many functions or
in case that they are very long. Having the prototype of all the functions
in the same place can spare us some time when determining how to call it or even ease
the creation of a header file.
<!--cuatut-->
<P>
<CENTER><TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 BORDER=0>
<TR><TD BGCOLOR="#0000FF"><IMG SRC="head0.gif" WIDTH=2 HEIGHT=2></TD></TR>
<TR><TD ALIGN="right"><FONT FACE="arial,helvetica" SIZE=1>© The C++ Resources Network, 2000-2003 - All rights reserved</FONT></TD></TR>
</TABLE></CENTER>
<P>
<CENTER>
<TABLE CELLPADDING=0 WIDTH=100%>
<TR><TD ALIGN="right" WIDTH=45%><A HREF="tut2-2.html">
<IMG SRC="butnback.gif" ALIGN="right" BORDER=0>
Previous:<BR><B>2-2. Functions (I).</B></A></TD>
<TD ALIGN="center" WIDTH=10%><A HREF="index.html">
<IMG SRC="butnindx.gif" BORDER=0><BR>
index</A></TD>
<TD ALIGN="left" WIDTH=45%><A HREF="tut3-1.html">
<IMG SRC="butnnext.gif" ALIGN="left" BORDER=0>
Next:<BR><B>3-1. Arrays. String of characters.</B></A>
</TD></TR></TABLE>
</CENTER>
<!--/cuatut-->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -