📄 tut2-3.html
字号:
<html>
<head>
<title>C++ Tutorial: 2.3, Functions (II).</title>
<META NAME="description" CONTENT="Passing parameters by reference and by value to functions. Default values. Overloaded, inline and recursive functions. Prototyping functions.">
<META NAME="keywords" CONTENT="argument &">
</head>
<body bgcolor="white">
<!--captut-->
<CENTER>
<TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=1 BORDER=0>
<TR><TD WIDTH=90%>
<FONT SIZE=4> Section 2.3 </FONT><BR>
<FONT SIZE=5><B> Functions (II). </B></FONT>
</TD><TD><!--ad--><!--#include virtual="/ad/ad468.shtml"--><!--/ad-->
</TD><TD VALIGN="bottom"><A HREF="http://www.cplusplus.com/doc/tutorial/">
<IMG SRC="head.gif" ALT="cplusplus.com" BORDER=0></A></TD></TR>
<TR><TD BGCOLOR="#0000FF" ALIGN="center" COLSPAN=3>
<IMG SRC="head0.gif" WIDTH=2 HEIGHT=2 BORDER=0></TD></TR>
</TABLE>
</CENTER>
<!--/captut-->
<p>
<h2>Arguments passed <i>by value</i> and <i>by reference</i>.</h2>
Until now, in all the functions we have seen, the parameters passed to the
functions have been passed <i>by value</i>. This means that when calling a function
with parameters, what we have passed to the function were <u>values</u> but
never the specified variables themselves. For example, suppose that we called
our first function <tt><b>addition</b></tt> using the following code :
<blockquote><tt>
int x=5, y=3, z;<br>
z = addition ( x , y );
</tt></blockquote>
What we did in this case was to call function <tt><b>addition</b></tt> passing
the <u>values</u> of <tt><b>x</b></tt> and <tt><b>y</b></tt>, that means
<tt><b>5</b></tt> and <tt><b>3</b></tt> respectively, not the variables themselves.
<blockquote>
<img src="imgfunc4.gif">
</blockquote>
<p>
This way, when function <tt><b>addition</b></tt> is being called the value of its variables
<tt><b>a</b></tt> and <tt><b>b</b></tt> become <tt><b>5</b></tt> and <tt><b>3</b></tt>
respectively, but any modification of <tt><b>a</b></tt> or <tt><b>b</b></tt> within the
function <tt><b>addition</b></tt> will not affect the values of <tt><b>x</b></tt> and
<tt><b>y</b></tt> outside it, because variables <TT><B>x</B></TT> and <TT><B>y</B></TT>
were not passed themselves to the the function, only their <U>values</U>.
<p>
But there might be some cases where you need to manipulate from inside a function the value of
an external variable. For that purpose we have to use <i>arguments passed by reference</i>,
as in the function <tt><b>duplicate</b></tt> of the following example:
<P>
<CENTER>
<TABLE WIDTH=100% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// passing parameters by reference</I>
#include <iostream.h>
void duplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}
int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top">
<B><TT>x=2, y=6, z=14</TT></B>
</TD></TR></TABLE>
</CENTER>
<P>
The first thing that should call your attention is that in the declaration of
<tt><b>duplicate</b></tt> the type of each argument was followed by an <i>ampersand</i>
sign (<tt><b>&</b></tt>), that serves to specify that the variable has to
be passed <i>by reference</i> instead of <i>by value</i>, as usual.
<p>
When passing a variable <i>by reference</i> we are passing the variable itself and any
modification that we do to that parameter within the function will have effect in the
passed variable outside it.
<p>
<img src="imgfunc3.gif">
<p>
To express it another way, we have associated <tt><b>a</b></tt>,
<tt><b>b</b></tt> and <tt><b>c</b></tt> with the parameters used when calling the function
(<tt><b>x</b></tt>, <tt><b>y</b></tt> and <tt><b>z</b></tt>) and any change that we do
on <tt><b>a</b></tt> within the function will affect the value of <tt><b>x</b></tt>
outside. Any change that we do on <tt><b>b</b></tt> will affect <tt><b>y</b></tt>,
and the same with <tt><b>c</b></tt> and <tt><b>z</b></tt>.
<p>
That is why our program's output, that shows the values stored in <tt><b>x</b></tt>,
<tt><b>y</b></tt> and <tt><b>z</b></tt> after the call to <tt><b>duplicate</b></tt>,
shows the values of the three variables of <tt><b>main</b></tt> doubled.
<p>
If when declaring the following function:<br>
<blockquote>
<tt><b>void duplicate (int& a, int& b, int& c)</b></tt><br>
</blockquote>
we had declared it thus:<br>
<blockquote>
<tt><b>void duplicate (int a, int b, int c)</b></tt><br>
</blockquote>
that is, without the <i>ampersand</i> (<tt><b>&</b></tt>) signs, we would have not
passed the variables <i>by reference</i>, but their values, and therefore, the output on
screen for our program would have been the values of <tt><b>x</b></tt>,
<tt><b>y</b></tt> and <tt><b>z</b></tt> without having been modified.
<p>
<table><tr><td bgcolor="#BFFFBF">
<img src="icoc-cpp.gif" align="left">
This type of declaration "<i>by reference</i>" using the
<i>ampersand</i> (<tt><b>&</b></tt>) sign is exclusive of C++. In C language we had to
use pointers to do something equivalent.
</td></tr></table>
<P>
Passing by reference is an effective way to allow a function to return more than
one single value. For example, here is a function that returns the previous and next
numbers of the first parameter passed.
<P>
<CENTER>
<TABLE WIDTH=100% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// more than one returning value</I>
#include <iostream.h>
void prevnext (int x, int& prev, int& next)
{
prev = x-1;
next = x+1;
}
int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top">
<B><TT>Previous=99, Next=101</TT></B>
</TD></TR></TABLE>
</CENTER>
<p>
<h2>Default values in arguments.</h2>
When declaring a function we can specify a default value for each parameter.
This value will be used if that parameter is left blank when calling to the function.
To do that we simply have to assign a value to the arguments in the function declaration.
If a value for that parameter is not passed when the function is called, the default
value is used, but if a value is specified this default value is stepped on and the passed
value is used.
For example:
<P>
<TABLE WIDTH=100% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// default values in functions</I>
#include <iostream.h>
int divide (int a, int b=2)
{
int r;
r=a/b;
return (r);
}
int main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top">
<B><TT>6<BR>5</TT></B>
</TD></TR></TABLE>
<P>
As we can see in the body of the program there are two calls to the function
<tt><B>divide</B></tt>. In the first one:<br>
<blockquote><tt><b>divide (12)</b></tt><br></blockquote>
we have only specified one argument, but the function <tt><b>divide</b></tt>
allows up to two. So the function <tt><b>divide</b></tt> has assumed
that the second parameter is <tt><b>2</b></tt> since that is what we have specified to
happen if this parameter is lacking (notice the function declaration, which finishes with
<tt><b>int b=2</b></tt>). Therefore the result of this function call is <b>6</b>
(<tt><b>12/2</b></tt>).
<p>
In the second call:<br>
<blockquote>
<tt><b>divide (20,4)</b></tt><br>
</blockquote>
there are two parameters, so the default assignation (<tt>int b=2</tt>) is stepped on
by the passed parameter, that is <tt><b>4</b></tt>, making the result equal to <b>5</b>
(<tt><b>20/4</b></tt>).
<p>
<h2>Overloaded functions.</h2>
Two different functions can have the same name if the prototype of their arguments are different,
that means that you can give the same name to more than one function if they have either a
different number of arguments or different types in their arguments. For example,
<P>
<CENTER>
<TABLE WIDTH=100% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// overloaded function</I>
#include <iostream.h>
int divide (int a, int b)
{
return (a/b);
}
float divide (float a, float b)
{
return (a/b);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -