It all depends.
If you want a function to pass values back, you either have the function return or pass the variables in by reference.
Pass by Reference
Passing parameters by reference means passing the address of the variable. Most languages wrap this up nicely so you never have to deal with addresses. But C, being what it is, doesn't have a reference mechanism and requires the programmer to pass the address of things whenever references are requiired.
This example passes a double by reference using pointers as you would do in C.
1 2 3 4 5 6 7 8 9 10 11
|
void init(double *pValue)
{
*pValue = 0.0;
}
int main()
{
double d;
init(&d); // pass d by reference by passing its address
return 0;
}
|
C++ does provide reference variables, so the use of a pointer is sort of hidden. Here's the equivalent example using pointers.
1 2 3 4 5 6 7 8 9 10 11
|
void init(double &Value)
{
Value = 0.0;
}
int main()
{
double d;
init(d); // pass d by reference
return 0;
}
|
New and Delete
In C, there's an area of free memory called the heap. If you need some memory, you can ask for some and if there's enough available, you'll be told where the memory block starts. When you're done, you need to give it back, so the heap can reuse that memory on a later request.
In C++, you ask for memory by calling
new
. And you give it back by calling
delete
.
It's an error to
delete
memory that was not obtained by using
new
.
Here's an example of how it may be used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <iostream>
int main()
{
size_t array_size = 0;
std::cout << "Please enter a size: ";
std::cin >> array_size;
int* array = new[array_size]; // ask for some memory
if (array != 0) // check if the request for memory succeeded
{
int sum = 0;
for (size_t i = 0; i != array_size; ++i)
{
std::cout << "value " << i << " : ";
std::cin >> array[i];
sum += array[i];
}
std::cout << "average is: " << sum/array_size << std::endl;
delete [] array; // give the memory back
}
else
std::clog << "out of memory" << std::endl;
return 0;
}
|