Pointers - 3.2 PARAMETER PASSING USING POINTERS

4.2 PARAMETER PASSING USING POINTERS

What do you understand by parameter passing? It is important when you use functions. How does parameter passing relate to pointers?
Pointers are normally passed to functions. This way, we pass the address to a local variable that points to the actual location of the data item by the calling program. The value that is changed in the function will change the value at the actual location. This method of passing is known as passing by reference or reference passing.

Program 4.1
#include
#include
 #include
#include
float calculateTotal1(int, int, int, float);
float calculateTotal2(int, int, int *, float *);

int main(){
int total1, total2, i, total;
float average1, average2, average;                 

total1 = total2 = 0;              
average1 = average2 = 0.0; 
for (i=1;i<=9;i++)
total +=1;
average = (float) total /8;
printf(" %d %.2f\n",i, average); 
         
calculateTotal1(1, 9, total1, average1);
printf("total1 = %d, average1 = %.2f\n", total1,average1);
calculateTotal2(1, 9, &total2, &average2);
printf("total2 = %d, average2 = %.2f\n", total2, average2);
getch();
}

float calculateTotal1(int start, int finish, int total, float average) {
int i;

for (i = start; i <= finish; i++)              
total +=i;
average = (float) total / (finish - start + 1);     
}

float calculateTotal2(int start, int finish, int *total, float *average) {                                    
int i;               

for (i = start; i <= finish; i++)                 
*total +=i;
*average = (float) *total / (finish - start + 1);

}
 
When the calculateTotal2() function is called, the address for total1  (which is &total1) is assigned to the total pointer. This means that total points to the actual location of total1 in the calling program statement. In the same way, the average pointer points to the location of average2. Figure 4.4 shown the relationship between variables when the calculateTotal2() function is called.
                         Output :  total 1 = 0, average1 =0.00    total2 = 45, average2 = 5.00
Figure 4.4: Relationship between variables during the calculateTotal2 function call
Then, the body of the calculateTotal2() function performs that calculation and changes the value for the variables that are pointed by total and average. This means that the actual value that is changed, is the value stored in the actual total2 location (that is pointed by total) and average2 (that is pointed by average). When this function ends, the values of total2 and average2 has been changed to the new values as calculated during the execution of that function.

EXERCISE 4.2

 Look at the outline of following function:
 #include
#include
#include
float function1(int a, int b);
float function2(int *p, int *q);

int main() {
int a=10; 
int b=5;
float x,y;
/* call function function2 & print on screen */
x = function1(a, b);
printf("function 1  ->a = %d, b = %d, x=%.2f\n", a, b, x);

/* call function function2 & print on screen */
y = function2(&a, &b);
printf("function 2 -> a = %.2f, b = %.2f, y=%.2f \n", &a, &b, y);
getch(); /* hold output screen, need include conio.h */
} /* end main */

/* function section */
float function1(int i, int j) {
i = 70; j = 7;
return ( (float) i / j);
}
float function2(int *i, int *j) {
*i = 50; *j = 4;
return ( (float) *i / *j);
}
 
  1. What is the value assigned to a and b after the program has completed its execution?
  2. What is the output for this program?
    FUNCTION 1 -> a = 10, b = 5, x =10.00
    FUNCTION 2 -> a = 0.00, b = 12.50, y =0.00
(Use your own format to answer this question)

Comments

Popular posts from this blog

How to Install ‘IPFire’ Free Firewall Linux Distribution

Installing Moodle on a Debian-based LAMP server

Working with Columns on the Joomla Frontpage