Lec18 Pointers Part2
Lec18 Pointers Part2
Pointers
Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
9.7
Pointers as Function Parameters
Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha © Makady.
Education, 2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
Passing Arguments to a Function
• In C++, there are three ways to pass
arguments to a function:
– Pass-by-value
– Pass-by-reference with reference arguments
– Pass-by-reference with pointer arguments.
• Pass-by-value
– When an argument is passed by value, a copy of
the argument is made, and passed to the function.
– Changes to the copy (inside the function) do NOT
affect the original variable.
Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha © Makady.
Education, 2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
Passing Arguments to a Function
• Pass-by-reference with reference arguments
• To pass by reference, you need to use
reference parameters.
• When passing by reference, you give the
function a permission to modify the parameters.
• To indicate that a parameter is passed by
reference, follow the parameter’s type by an
ampersand (&) in the functions’
header/prototype.
Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
• Pass-by-value
int squareByValue(int);
int main()
{ int x = 2;
cout<<" x = "<< x <<" before the
squareByValue\n";
cout<<“squareByValue:
"<<squareByValue(x)<<"\n";
cout<<" x = "<< x <<" after the squareByValue\n";
return 0;
}
int squareByValue(int number)
{
Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha © Makady.
Education, 2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
• Pass-by-value
int squareByValue(int);
int main()
{ int x = 2;
cout<<" x = "<< x <<" before the
squareByValue\n";
cout<<“squareByValue:
"<<squareByValue(x)<<"\n";
cout<<" x = "<< x <<" after the squareByValue\n";
return 0;
}
x = 2 before the squareByValue
Value returned by squareByValue: 4
int squareByValue(int number)
x = 2 after the squareByValue
{
Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha © Makady.
Education, 2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
• Pass-by-reference with reference arguments
void squareByReference(int &);
int main()
{
int x = 2;
cout<<" x = "<< x <<" before
squareByReference\n";
squareByReference(x);
cout<<" x = "<< x <<" after squareByReference\n";
return 0;}
void squareByReference(int &numberRef)
{
numberRef *= numberRef;
Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha © Makady.
Education, 2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
• Pass-by-reference with reference arguments
void squareByReference(int &);
int main()
{
int x = 2;
cout<<" x = "<< x <<" before
squareByReference\n";
squareByReference(x);
cout<<" x = "<< x <<" after squareByReference\n";
return 0;}
x = 2 before the squareByReference
void squareByReference(int &numberRef)
{ x = 4 after the squareByReference
numberRef *= numberRef;
Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha © Makady.
Education, 2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
• Pass-by-reference with pointer arguments
void squareByReference_withPointer(int *);
int main()
{ int n = 2;
cout<<" n = "<< n <<" before
squareByReference_withPointer\n";
squareByReference_withPointer(&n);
cout<<" n = "<< n <<" after the
squareByReference_withPointer\n";
return 0;}
void squareByReference_withPointer(int *nPtr)
{
*nPtr = *nPtr * *nPtr;
Copyright © 2016
Copyright
Soha © Makady.
2016 Soha
All rights
Makady.
reserved.
All rights reserved.
} Copyright © 2012 Pearson Education, Inc.
• Pass-by-reference with pointer arguments
void squareByReference_withPointer(int *);
int main()
{ int n = 2;
cout<<" n = "<< n <<" before
squareByReference_withPointer\n";
squareByReference_withPointer(&n);
cout<<" n = "<< n <<" after the
squareByReference_withPointer\n";
return 0;}
void
n = 2squareByReference_withPointer(int
before the squareByReference_withPointer *nPtr)
{n = 4 after the squareByReference_withPointer
*nPtr = *nPtr * *nPtr;
Copyright © 2016
Copyright
Soha © Makady.
2016 Soha
All rights
Makady.
reserved.
All rights reserved.
} Copyright © 2012 Pearson Education, Inc.
Pointers to Constants
• If we want to store the address of a
constant in a pointer, then we need to
store it in a pointer-to-const.
9-19
Copyright © 2012 Pearson Education, Inc.
Pointers to Constants
• Suppose we wish to pass the payRates array to
a function? Here's an example of how we can do
it.
• Example