 Arrays, Pointers, and Classes

 Pointers as Arguments

 Introduction

As we have seen so far, a function can use one or more arguments in order to carry its assignment. When necessary, a function also declares its own variable(s) to get the desired return value. Here is an example:

```#using <mscorlib.dll>
using namespace System;

double  CalculateNetPrice(double Disc);

int main()
{
double FinalPrice;
double Discount = 20;

FinalPrice = CalculateNetPrice(Discount);

Console::WriteLine("After applying a 20% discount");
Console::Write("Final Price = ");
Console::WriteLine(FinalPrice.ToString("C"));

Console::WriteLine();
return 0;
}

double  CalculateNetPrice(double Discount)
{
double OriginalPrice;

Console::Write("Please enter the original price: ");
OriginalPrice = OrigPrice->ToDouble(0);

return OriginalPrice - (OriginalPrice * Discount / 100);
}```

Like other variables, a pointer can be passed to a function. When declaring  and when implementing a function that takes a pointer as an argument, use the asterisk for the argument or for each argument. Here is an example:

```#using <mscorlib.dll>
using namespace System;

double  CalculateNetPrice(double *Disc);

int main()
{
Console::WriteLine();
return 0;
}

double  CalculateNetPrice(double *Discount)
{
double OriginalPrice;

Console::Write("Please enter the original price: ");
OriginalPrice = OrigPrice->ToDouble(0);

return OriginalPrice - (OriginalPrice * *Discount / 100);
}```

When calling the function, use the reference(s) to the variable(s). The function will perform its assignment on the referenced variable(s). After the function has performed its assignment, the changed value(s) of the argument(s) will be preserved and given to the calling function. Here is an example:

```int main()
{
double FinalPrice;
double Discount = 20;

FinalPrice = CalculateNetPrice(&Discount);

Console::WriteLine("After applying a 20% discount");
Console::Write("Final Price = ");
Console::WriteLine(FinalPrice.ToString("C"));

Console::WriteLine();
return 0;
}```

An example of running the program is:

```Please enter the original price: 155.55
After applying a 20% discount
Final Price = \$124.44

Press any key to continue```

 The Effect of Passing a Pointer as Argument

Consider the following program:

```#using <mscorlib.dll>
using namespace System;

void GetTheOriginalPrice(double OrigPrice);

int main()
{
double OriginalPrice = 0;

Console::WriteLine("First in main() --");
Console::Write("Original Price =  \$");
Console::WriteLine(OriginalPrice);

GetTheOriginalPrice(OriginalPrice);

Console::WriteLine("\nBack in main() --");
Console::Write("Original Price =  \$");
Console::WriteLine(OriginalPrice);

Console::WriteLine();
return 0;
}

void GetTheOriginalPrice(double OrigPrice)
{
Console::WriteLine("\nNow we are in the GetTheOriginalPrice() function");
Console::Write("Please enter the original price: ");

OrigPrice = Price->ToDouble(0);

Console::WriteLine("\nIn the GetTheOriginalPrice() function");
Console::Write("Original Price = \$");
Console::WriteLine(OrigPrice);
}
```

Here is an example of running the program:

```First in main() --
Original Price =  \$0

Now we are in the GetTheOriginalPrice() function
Please enter the original price: 100

In the GetTheOriginalPrice() function
Original Price = \$100

Back in main() --
Original Price =  \$0

Press any key to continue...```

Notice that the value of the OriginalPrice variable is kept intact in the main() function, as 0, before and after calling the GetTheOriginalPrice() function.

Like a reference, when passing a pointer as argument to a function, the function that is receiving the argument is in fact accessing the argument's address. Therefore, like a reference, the called function has the ability to alter the value held by the pointer. The effect is the same as for the reference: if the called function modifies the value of the pointer, that value is permanently changed. This is a feature you can use to your advantage. This effect is illustrated in the following program:

 ```#using using namespace System; void GetTheOriginalPrice(double *OrigPrice); int main() { double OriginalPrice = 0; Console::WriteLine("First in main() --"); Console::Write("Original Price = \$"); Console::WriteLine(OriginalPrice); GetTheOriginalPrice(&OriginalPrice); Console::WriteLine("\nBack in main() --"); Console::Write("Original Price = \$"); Console::WriteLine(OriginalPrice); Console::WriteLine(); return 0; } void GetTheOriginalPrice(double *OrigPrice) { Console::WriteLine("\nNow we are in the GetTheOriginalPrice() function"); Console::Write("Please enter the original price: "); String *Price = Console::ReadLine(); *OrigPrice = Price->ToDouble(0); Console::WriteLine("\nIn the GetTheOriginalPrice() function"); Console::Write("Original Price = \$"); Console::WriteLine(*OrigPrice); }```

Here is an example of executing this program:

 ```First in main() -- Original Price = \$0 Now we are in the GetTheOriginalPrice() function Please enter the original price: 125.55 In the GetTheOriginalPrice() function Original Price = \$125.55 Back in main() -- Original Price = \$125.55 Press any key to continue```

Notice that, this time, after calling the GetTheOriginalPrice() function, the value of the OriginalPrice variable is permanently changed and the second time it is accessed in the main() function, it holds a different value than the first time it was called.

 Constant Pointers as Arguments

The previous section demonstrates to us that, when passing a pointer as argument, the effect is the same as passing an argument as reference. This shows that, passing a pointer as argument gives the called function direct access to the address of the variable. Besides permanently changing the value of the argument, this process also speeds up code execution because the called function does not deal with a copy of the variable but the variable itself. Although there are various good reasons to pass pointers as arguments, sometimes you may not want the called function to modify the value held by the variable. In fact you can prevent this.

If a function that receives a pointer as argument is not supposed to modify the value of the argument, you can pass the argument as a constant pointer. To do this, type the const keyword on the left side of the data type of the pointer argument. Here is an example:

```#using <mscorlib.dll>
using namespace System;

double  CalculateNetPrice(const double *Disc);

int main()
{
double FinalPrice;
double Discount = 20;

FinalPrice = CalculateNetPrice(&Discount);

Console::WriteLine("After applying a 20% discount");
Console::Write("Final Price = ");
Console::WriteLine(FinalPrice.ToString("C"));

Console::WriteLine();
return 0;
}

double  CalculateNetPrice(const double *Discount)
{
double OriginalPrice;

Console::Write("Please enter the original price: ");
OriginalPrice = OrigPrice->ToDouble(0);

return OriginalPrice - (OriginalPrice * *Discount / 100);
}```

Here is an example of running the program:

```Please enter the original price: 250.55
After applying a 20% discount
Final Price = \$200.44

Press any key to continue```