Recapitulare privind transmiterea parametrilor

Parametrii unei functii pot fi transmisi in 3 moduri:

  1. prin valoare
  2. prin pointeri
  3. prin referinta

Analog o functie poate returna mai multe valori folsind fie pinteri fie referinte

Transmiterea parametrilor prin referinte

Codul urmator (ref_pass.cpp) ilustreaza acest mod de transmitere a parametrilor

#include <iostream.h>
void swap(int &x, int &y);
int main()
{
int x = 5, y = 10;

cout << "Main. Before swap, x: " << x << " y: " << y << "\n";
swap(x,y);
cout << "Main. After swap, x: " << x << " y: " << y << "\n";
return 0;
}

void swap (int &rx, int &ry)
//aici rx si ry devin aliasuri ("porecle") respectiv pentru x si y
{
int temp;

cout << "Swap. Before swap, rx: " << rx << " ry: " << ry << "\n";

temp = rx;
rx = ry;
ry = temp;

cout << "Swap. After swap, rx: " << rx << " ry: " << ry << "\n";
}

Dupa executie aceasta functie returneaza

Main. Before swap, x: 5 y: 10
Swap. Before swap, rx: 5 ry: 10
Swap. After swap, rx: 10 ry: 5
Main. After swap, x: 10 y: 5

Acelasi rezultat il are si urmatorul exemplu (poi_pass.cpp).

Transmiterea parametrilor prin pointeri

//Transmiterea parametrilor prin pointeri

#include <iostream.h>
void swap(int *x, int *y);
int main()
{
int x = 5, y = 10;

cout << "Main. Before swap, x: " << x << " y: " << y << "\n";
swap(&x,&y);
cout << "Main. After swap, x: " << x << " y: " << y << "\n";
return 0;
}

void swap (int *rx, int *ry)
//aici rx si ry sunt pointeri catre x si y
{
int temp;

cout << "Swap. Before swap, rx: " << *rx << " ry: " << *ry << "\n";

temp = *rx;
*rx = *ry;
*ry = temp;

cout << "Swap. After swap, rx: " << *rx << " ry: " << *ry << "\n";
}

Intoarcerea mai multor valori prin referinte (ref_retv.cpp)

// Intoarcerea mai multor valori prin referinte
#include <iostream.h>
typedef unsigned short USHORT;
short Factor(USHORT, USHORT&, USHORT&);
int main()
{
USHORT number, squared, cubed;
short error;
cout << "Enter a number (0 - 20): ";
cin >> number;
error = Factor(number, squared, cubed);
if (!error)
{
cout << "number: " << number << "\n";
cout << "square: " << squared << "\n";
cout << "cubed: " << cubed << "\n";
}
else
cout << "Error encountered!!\n";
return 0;
}
short Factor(USHORT n, USHORT &pSquared, USHORT &pCubed)
{
short Value = 0;
if (n > 20)
Value = 1;
else
{
pSquared = n*n;
pCubed = n*n*n;
Value = 0;
}
return Value;
}

Acest cod determina dupa executie urmatoarea stare a terminalului

Enter a number (0 - 20): 17
number: 17
square: 289
cubed: 4913

Acelasi rezultat il genereaza si urmatorul exemplu.

Intoarcerea mai multor valori cu pointeri (poi_retv.cpp)

#include <iostream.h>
typedef unsigned short USHORT;
short Factor(USHORT, USHORT*, USHORT*);
int main()
{
USHORT number, squared, cubed;
short error;
cout << "Enter a number (0 - 20): ";
cin >> number;
error = Factor(number, &squared, &cubed);
if (!error)
{
cout << "number: " << number << "\n";
cout << "square: " << squared << "\n";
cout << "cubed: " << cubed << "\n";
}
else
cout << "Error encountered!!\n";
return 0;
}
short Factor(USHORT n, USHORT *pSquared, USHORT *pCubed)
{
short Value = 0;
if (n > 20)
Value = 1;
else
{
*pSquared = n*n;
*pCubed = n*n*n;
Value = 0;
}
return Value;
}

 

Cornel Mironel Niculae, 2003-2004

04-Mar-2004