0% found this document useful (0 votes)
36 views8 pages

CPE221 PointersFunctions

The document discusses call by reference in C++ functions. In call by reference, the addresses of variables in the calling function are passed to the called function rather than copies of the variable values. This allows the called function to directly modify the original variables in the calling function by dereferencing the passed pointers. The document provides an example where a function gets cylinder dimension values from the user by call by reference, writing the entered radius and height back to the original variables in the main function.

Uploaded by

adelaja qowiyyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views8 pages

CPE221 PointersFunctions

The document discusses call by reference in C++ functions. In call by reference, the addresses of variables in the calling function are passed to the called function rather than copies of the variable values. This allows the called function to directly modify the original variables in the calling function by dereferencing the passed pointers. The document provides an example where a function gets cylinder dimension values from the user by call by reference, writing the entered radius and height back to the original variables in the main function.

Uploaded by

adelaja qowiyyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

POINTERS

&
FUNCTIONS
Dr. Hisham Muhammad
Department of Electrical and Electronics Engineering
University of Lagos.
Pointers and Functions
Call by Reference: Variable’s Address to a Function (1)

In a call by reference, the address of the data variable is passed to a


function.
The address is copied into a pointer variable that belongs to the called
function.
The called function accesses the calling function variable via indirection
operator and pointer.
The address of the variable can be thought of as a reference to the variable
This technique makes the calling function’s variable available to the called
function, and the called function does not have its own local copy of the
variable.
Call by Reference: Variable’s Address to a Function (2)

If a called function must return several data items to a calling function, the
programmer can use the call by reference technique.
The calling function passes the addresses of its variables to the called
function, which then writes the data directly into the calling function’s
variables.
In the Calculate Cylinder Volume program, (5-9), we want to use a
GetCylinderDimensions function to ask the user to enter the radius and
height
To return two pieces of data to main, we shall use a call by reference. Figure
(a) – (d) illustrates
Call by Reference: Variable’s Address to a Function (3)
Program 5-9 Calculate Cylinder Volume 2nd Version
// Call by Reference using pointers to functions.
void GetCylinderDimensions(float *r_ptr, float *h_ptr)
#include <iostream> //pointers
const double pi = 3.14159265; {
float R, H;
void GetCylinderDimensions ( float*, float*);
cout << “\n Enter radius and hieight “;
//Prototypes pointers here
cin >> R >> H;
float CalcCylinderVolume ( float, float);
*r_ptr = R;
// Call by value here
*h_ptr = H;
int main() }
{
float radius, height, volume; float CalcCylinderVolume(float rad, float hgt)
GetCylinderDimensions(&radius, &height); {
//send addresses return (pi * rad * rad * hgt);
volume = CalcCylinderVolume(radius,height); //shortcut by doing calcs in return ()
//send values
}
cout << "\n Cylinder Volume = " << volume << endl;
}
Call by Reference: Variable’s Address to a Function (1)
main’s variables
int main()
volume height radius
{
float radius, height, volume;
GetCylinderDimensions(&radius, &height); Addresses: xFF10 xFF14 xFF18

//rest of main

}
xFF14
xFF18

void GetCylinderDimensions(float *r_ptr, float *h_ptr)


GetCylinderDimensions’ variables {
H R r_ptr h_ptr
float R, H;
xFF18 xFF14
}

Step 1: The addresses of the radius and the height are passed to the function and
stored in the pointer variables (a) Step 1
Call by Reference: Variable’s Address to a Function (2)
main’s variables
int main() height
volume radius
{
float radius, height, volume;
GetCylinderDimensions(&radius, &height); Addresses: xFF10 xFF14 xFF18

//rest of main

GetCylinderDimensions’ variables void GetCylinderDimensions(float *r_ptr, float *h_ptr)


r_ptr h_ptr {
H R
float R, H;
15.00000 10.00000 xFF18 xFF14 cout << “\n Enter radius and height.”;
cin >> R >> H;
}

User enters 10.0 for radius and 15.0 for height

Step 2: The values of the radius and the height are obtained and stored in local variables
(b) Step 2
Call by Reference: Variable’s Address to a Function (3)
main’s variables
int main() volume height radius
{
float radius, height, volume; 10.00000

GetCylinderDimensions(&radius, &height); Addresses: xFF10 xFF14 xFF18

//rest of main

void GetCylinderDimensions(float *r_ptr, float *h_ptr)


{
float R, H;
GetCylinderDimensions’ variables
cout << “\n Enter radius and height”;
H R r_ptr h_ptr cin >> R >> H;

15.00000 10.00000 xFF18 xFF14 *r_ptr = R; // put R where r_ptr is pointing

Step 3: The value in R is assigned where r_ptr is pointing. The radius value of
10.00000 is written into main’s radius variable (c) Step 3
Call by Reference: Variable’s Address to a Function (4)
main’s variables
int main() volume height radius
{
float radius, height, volume; 15.00000 10.00000

GetCylinderDimensions(&radius, &height); Addresses: xFF10 xFF14 xFF18

//rest of main

void GetCylinderDimensions(float *r_ptr, float *h_ptr)


{
GetCylinderDimensions’ variables float R, H;
cout << “\n Enter radius and height”;
r_ptr h_ptr cin >> R >> H;
H R
15.00000 10.00000 xFF18 xFF14 *r_ptr = R; // put R where r_ptr is pointing
*h_ptr = H; // now put H where h_ptr is pointing
}

Step 4: The value in H is assigned where h_ptr is pointing. The height value of
15.00000 is written into main’s height variable (d) Step 4

You might also like