A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2FFunction%2Ffunction_parameters</id>
		<title>C++ Tutorial/Function/function parameters - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2FFunction%2Ffunction_parameters"/>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Function/function_parameters&amp;action=history"/>
		<updated>2026-04-17T15:18:42Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Function/function_parameters&amp;diff=2475&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Function/function_parameters&amp;diff=2475&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:17Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Function/function_parameters&amp;diff=2476&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Function/function_parameters&amp;diff=2476&amp;oldid=prev"/>
				<updated>2010-05-25T10:29:48Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Change a call-by-value parameter does not affect the argument==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
double f(double x); &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  double t = 10.0; &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;1/10.0 is &amp;quot; &amp;lt;&amp;lt; f(t) &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Value of t is still: &amp;quot; &amp;lt;&amp;lt; t &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
double f(double x) &lt;br /&gt;
{ &lt;br /&gt;
  x = 1 / x;&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;inside f &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  return x; &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;inside f 0.1&lt;br /&gt;
1/10.0 is 0.1&lt;br /&gt;
Value of t is still: 10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Change the contents of an array using a function==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
void f(int *n, int num); &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  int i, nums[10]; &lt;br /&gt;
 &lt;br /&gt;
  for(i=0; i &amp;lt; 10; i++) nums[i] = i+1; &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Original contents: &amp;quot;; &lt;br /&gt;
  for(i=0; i &amp;lt; 10; i++) cout &amp;lt;&amp;lt; nums[i] &amp;lt;&amp;lt; &amp;quot; &amp;quot;; &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  f(nums, 10); // compute cubes &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Altered contents: &amp;quot;; &lt;br /&gt;
  for(i=0; i&amp;lt;10; i++) cout &amp;lt;&amp;lt; nums[i] &amp;lt;&amp;lt; &amp;quot; &amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
void f(int *n, int num) &lt;br /&gt;
{ &lt;br /&gt;
  while(num) { &lt;br /&gt;
    *n = *n * *n ; &lt;br /&gt;
    num--; &lt;br /&gt;
    n++; &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Original contents: 1 2 3 4 5 6 7 8 9 10&lt;br /&gt;
Altered contents: 1 4 9 16 25 36 49 64 81 100&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Declare int array parameter for a function without indicating the array length==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
void display(int num[]); &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  int t[10], i; &lt;br /&gt;
 &lt;br /&gt;
  for(i=0; i &amp;lt; 10; ++i) t[i]=i; &lt;br /&gt;
 &lt;br /&gt;
  display(t); // pass array t to a function &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void display(int num[]) &lt;br /&gt;
{ &lt;br /&gt;
  int i; &lt;br /&gt;
 &lt;br /&gt;
  for(i=0; i &amp;lt; 10; i++) cout &amp;lt;&amp;lt; num[i] &amp;lt;&amp;lt; &amp;quot; &amp;quot;; &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;0 1 2 3 4 5 6 7 8 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Define function to accept three int parameters==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
void box(int length, int width, int height); // box()&amp;quot;s prototype &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  box(7, 20, 4); &lt;br /&gt;
  box(50, 3, 2); &lt;br /&gt;
  box(8, 6, 9); &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
void box(int length, int width, int height) &lt;br /&gt;
{ &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;volume of box is &amp;quot; &amp;lt;&amp;lt; length * width * height &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;volume of box is 560&lt;br /&gt;
volume of box is 300&lt;br /&gt;
volume of box is 432&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Demonstrate the pointer version of swap(): Exchange the values  of the variables pointed to by x and y==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
void swap(int *x, int *y); &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  int i, j; &lt;br /&gt;
 &lt;br /&gt;
  i = 10; &lt;br /&gt;
  j = 20; &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Initial values of i and j: &amp;quot;; &lt;br /&gt;
  cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; j &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  swap(&amp;amp;j, &amp;amp;i); // call swap() with addresses of i and j &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Swapped values of i and j: &amp;quot;; &lt;br /&gt;
  cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; j &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
void swap(int *x, int *y) &lt;br /&gt;
{ &lt;br /&gt;
  int temp; &lt;br /&gt;
 &lt;br /&gt;
  temp = *x;&lt;br /&gt;
  *x = *y;  &lt;br /&gt;
  *y = temp;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Initial values of i and j: 10 20&lt;br /&gt;
Swapped values of i and j: 20 10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Function parameter: Use int pointer to accept an array==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
void display(int num[]); &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  int t[10], i; &lt;br /&gt;
 &lt;br /&gt;
  for(i=0; i &amp;lt; 10; ++i) t[i]=i; &lt;br /&gt;
 &lt;br /&gt;
  display(t); // pass array t to a function &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
void display(int *num) &lt;br /&gt;
{ &lt;br /&gt;
  int i; &lt;br /&gt;
 &lt;br /&gt;
  for(i=0; i &amp;lt; 10; i++) cout &amp;lt;&amp;lt; num[i] &amp;lt;&amp;lt; &amp;quot; &amp;quot;; &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;0 1 2 3 4 5 6 7 8 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Handling an array parameter as a pointer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using std::cout;&lt;br /&gt;
using std::endl;&lt;br /&gt;
double average(double* array, int count);     &lt;br /&gt;
int main() {&lt;br /&gt;
  double values[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Average = &amp;quot;&amp;lt;&amp;lt; average(values, (sizeof values)/(sizeof values[0]))&amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
double average(double* array, int count) {&lt;br /&gt;
  double sum = 0.0;                           &lt;br /&gt;
  for(int i = 0 ; i &amp;lt; count ; i++)&lt;br /&gt;
    sum += *array++;                          &lt;br /&gt;
  return sum/count;                           &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Average = 5.5&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Pass a pointer to a function.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
void f(int *j);&lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  int i; &lt;br /&gt;
  int *p; &lt;br /&gt;
 &lt;br /&gt;
  p = &amp;amp;i; &lt;br /&gt;
 &lt;br /&gt;
  f(p);   // pass a pointer &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; i;&lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
void f(int *j) &lt;br /&gt;
{ &lt;br /&gt;
  *j = 100; &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;100&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Pass a string to a function: Invert the case of the letters within a string==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
#include &amp;lt;cstring&amp;gt; &lt;br /&gt;
#include &amp;lt;cctype&amp;gt;  &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
void f(char *str); &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  char str[80]; &lt;br /&gt;
 &lt;br /&gt;
  strcpy(str, &amp;quot;ABCD&amp;quot;); &lt;br /&gt;
 &lt;br /&gt;
  f(str); &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; str; &lt;br /&gt;
  return 0; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
void f(char *str) &lt;br /&gt;
{ &lt;br /&gt;
  while(*str) { &lt;br /&gt;
    if(isupper(*str)) &lt;br /&gt;
        *str = tolower(*str);  &lt;br /&gt;
    else if(islower(*str)) &lt;br /&gt;
        *str = toupper(*str); &lt;br /&gt;
    str++; &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;abcd&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Passing an array to a function==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using std::cout;&lt;br /&gt;
using std::endl;&lt;br /&gt;
double average(double array[], int count);    &lt;br /&gt;
int main() {&lt;br /&gt;
  double values[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Average = &amp;quot;&lt;br /&gt;
       &amp;lt;&amp;lt; average(values, (sizeof values)/(sizeof values[0]))&lt;br /&gt;
       &amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
double average(double array[], int count) {&lt;br /&gt;
  double sum = 0.0;                        &lt;br /&gt;
  for(int i = 0 ; i &amp;lt; count ; i++)&lt;br /&gt;
    sum += array[i];                       &lt;br /&gt;
  return sum/count;                        &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Average = 5.5&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Passing a two-dimensional array to a function==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using std::cout;&lt;br /&gt;
using std::endl;&lt;br /&gt;
double f(double values[][4], int n);&lt;br /&gt;
int main() {&lt;br /&gt;
  double beans[3][4] = {&lt;br /&gt;
                         { 1.0,  2.0,  3.0,  4.0},&lt;br /&gt;
                         { 5.0,  6.0,  7.0,  8.0},&lt;br /&gt;
                         { 9.0, 10.0, 11.0, 12.0}&lt;br /&gt;
                       };&lt;br /&gt;
  cout &amp;lt;&amp;lt; f(beans, sizeof beans/sizeof beans[0])&amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
double f(double array[][4], int size) {&lt;br /&gt;
  double sum = 0.0;&lt;br /&gt;
  for(int i = 0 ; i &amp;lt; size ; i++)       &lt;br /&gt;
    for(int j = 0 ; j &amp;lt; 4 ; j++)        &lt;br /&gt;
      sum += array[i][j];&lt;br /&gt;
  return sum;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;78&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Passing int by value==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 void swap(int x, int y);&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
     int x = 5, y = 10;&lt;br /&gt;
     std::cout &amp;lt;&amp;lt; &amp;quot;Main. Before swap, x: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot; y: &amp;quot; &amp;lt;&amp;lt; y &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
     swap(x,y);&lt;br /&gt;
     std::cout &amp;lt;&amp;lt; &amp;quot;Main. After swap, x: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot; y: &amp;quot; &amp;lt;&amp;lt; y &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void swap (int x, int y)&lt;br /&gt;
 {&lt;br /&gt;
     int temp;&lt;br /&gt;
     std::cout &amp;lt;&amp;lt; &amp;quot;Swap. Before swap, x: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot; y: &amp;quot; &amp;lt;&amp;lt; y &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
     temp = x;&lt;br /&gt;
     x = y;&lt;br /&gt;
     y = temp;&lt;br /&gt;
     std::cout &amp;lt;&amp;lt; &amp;quot;Swap. After swap, x: &amp;quot; &amp;lt;&amp;lt; x &amp;lt;&amp;lt; &amp;quot; y: &amp;quot; &amp;lt;&amp;lt; y &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
 }&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Main. Before swap, x: 5 y: 10&lt;br /&gt;
Swap. Before swap, x: 5 y: 10&lt;br /&gt;
Swap. After swap, x: 10 y: 5&lt;br /&gt;
Main. After swap, x: 5 y: 10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Pass int array to a function==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
void display(int num[10]); &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  int t[10], i; &lt;br /&gt;
 &lt;br /&gt;
  for(i=0; i &amp;lt; 10; ++i) t[i]=i; &lt;br /&gt;
 &lt;br /&gt;
  display(t); // pass array t to a function &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
void display(int num[10]) &lt;br /&gt;
{ &lt;br /&gt;
  int i; &lt;br /&gt;
 &lt;br /&gt;
  for(i=0; i &amp;lt; 10; i++) cout &amp;lt;&amp;lt; num[i] &amp;lt;&amp;lt; &amp;quot; &amp;quot;; &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;0 1 2 3 4 5 6 7 8 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Pass variable address to a function==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
void f(int *j); &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  int i; &lt;br /&gt;
 &lt;br /&gt;
  f(&amp;amp;i);  &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; i; &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
} &lt;br /&gt;
    &lt;br /&gt;
void f(int *j) &lt;br /&gt;
{ &lt;br /&gt;
  *j = 100; // var pointed to by j is assigned 100 &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;100&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==the use of ... and its support macros va_arg, va_start, and va_end==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdarg.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
void vsmallest(char *message, ...);&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
 vsmallest(&amp;quot;Print %d integers, %d %d %d&amp;quot;,10,4,1);&lt;br /&gt;
 return(0);&lt;br /&gt;
}&lt;br /&gt;
void vsmallest(char *message, ...)&lt;br /&gt;
{&lt;br /&gt;
 int inumber_of_percent_ds=0;&lt;br /&gt;
 va_list type_for_ellipsis;&lt;br /&gt;
 int ipercent_d_format = &amp;quot;d&amp;quot;;&lt;br /&gt;
 char *pchar;&lt;br /&gt;
 pchar=strchr(message,ipercent_d_format);&lt;br /&gt;
  &lt;br /&gt;
 while(*++pchar != &amp;quot;\0&amp;quot;) {&lt;br /&gt;
   pchar++;&lt;br /&gt;
   pchar=strchr(pchar,ipercent_d_format);&lt;br /&gt;
   inumber_of_percent_ds++;&lt;br /&gt;
 }&lt;br /&gt;
 printf(&amp;quot;print %d integers,&amp;quot;,inumber_of_percent_ds);&lt;br /&gt;
 va_start(type_for_ellipsis,message);&lt;br /&gt;
 while(inumber_of_percent_ds--)&lt;br /&gt;
   printf(&amp;quot; %d&amp;quot;,va_arg(type_for_ellipsis,int));&lt;br /&gt;
 va_end(type_for_ellipsis);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use array as function&amp;quot;s parameter==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream.h&amp;gt;&lt;br /&gt;
float Total(float a[],int num);&lt;br /&gt;
const int SIZE = 10;&lt;br /&gt;
main()&lt;br /&gt;
{&lt;br /&gt;
       float * f = new float [SIZE];&lt;br /&gt;
       for (int i=0;i&amp;lt;SIZE;i++)&lt;br /&gt;
               f[i]=i+i;  //*(f+i)&lt;br /&gt;
       cout &amp;lt;&amp;lt;&amp;quot;Sum = &amp;quot;  &amp;lt;&amp;lt; Total( f ,SIZE) &amp;lt;&amp;lt;endl;&lt;br /&gt;
       cout &amp;lt;&amp;lt; &amp;quot;Average = &amp;quot; &amp;lt;&amp;lt; Total(f,SIZE)/SIZE;&lt;br /&gt;
       delete []f;&lt;br /&gt;
       return 0 ;&lt;br /&gt;
}&lt;br /&gt;
float Total(float a[],int num)&lt;br /&gt;
{&lt;br /&gt;
       int i;&lt;br /&gt;
       float sum = 0;&lt;br /&gt;
       for (i=0; i&amp;lt;num ; i++)&lt;br /&gt;
               sum += a[i];&lt;br /&gt;
       return sum;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Sum = 90&lt;br /&gt;
Average = 9&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Using reference parameters==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using std::cout;&lt;br /&gt;
using std::endl;&lt;br /&gt;
int larger(int&amp;amp; m, int&amp;amp; n);&lt;br /&gt;
int main() {&lt;br /&gt;
  int value1 = 10;&lt;br /&gt;
  int value2 = 20;&lt;br /&gt;
  cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; larger(value1, value2) &amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
int larger(int&amp;amp; m, int&amp;amp; n) {&lt;br /&gt;
  return m &amp;gt; n ? m : n;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;20&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==var args has to be the last one==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;cstdio&amp;gt;&lt;br /&gt;
#include &amp;lt;cstdarg&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
bool debug = false;&lt;br /&gt;
void debugOut(char* str, ...)&lt;br /&gt;
{&lt;br /&gt;
  va_list ap;&lt;br /&gt;
  if (debug) {&lt;br /&gt;
    va_start(ap, str);&lt;br /&gt;
    vfprintf(stderr, str, ap);&lt;br /&gt;
    va_end(ap);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
void printInts(int num, ...)&lt;br /&gt;
{&lt;br /&gt;
  int temp;&lt;br /&gt;
  va_list ap;&lt;br /&gt;
  va_start(ap, num);&lt;br /&gt;
  for (int i = 0; i &amp;lt; num; i++) {&lt;br /&gt;
    temp = va_arg(ap, int);&lt;br /&gt;
    cout &amp;lt;&amp;lt; temp &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
  va_end(ap);&lt;br /&gt;
  cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
int main(int argc, char** argv)&lt;br /&gt;
{&lt;br /&gt;
  debug = true;&lt;br /&gt;
  debugOut(&amp;quot;int %d\n&amp;quot;, 5);&lt;br /&gt;
  debugOut(&amp;quot;String %s and int %d\n&amp;quot;, &amp;quot;hello&amp;quot;, 5);&lt;br /&gt;
  debugOut(&amp;quot;Many ints: %d, %d, %d, %d, %d\n&amp;quot;, 1, 2, 3, 4, 5);&lt;br /&gt;
  printInts(5, 5, 4, 3, 2, 1);&lt;br /&gt;
  return (0);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>