Page 73 - DCAP407_DATA_STRUCTURE
P. 73

Data Structure




                                      Advantages of Pass by Value or Call by Value
                                      1.  In Pass by value, expressions are passed as arguments instead of passing the values
                                          or variables as arguments.
                                      2.  This protects the values of the actual variables from getting altered by the called
                                          function.

                                      Disadvantages of Pass by Value or Call by Value
                                      1.  Pass by value technique will not allow the information to be sent back to the calling
                                          portion of the program.

                                      2.  It allows only one-way transfer of information i.e., from the calling function to the
                                          called function.




                                      Write a C program to swap two numbers using pointers.


                          Pass by Reference or Call by Reference

                          When an address is passed to a function, the parameters receiving the address will have to be pointers.
                          The process of calling a function by using pointers to pass the address of the variable is called pass by
                          reference or call by reference. The function called by reference changes the values of the variable used in
                          the call.

                                         #include<stdio.h>
                                         void num(int *x, int *y)
                                         {
                                          *x = 100;
                                          *y = 200;
                                         }
                                         void main()
                                         {
                                            int m, n;
                                            m = 10;
                                            n = 20;
                                            num(&m, &n);
                                            printf( “ m = %d and n= %d”, m, n);
                                           getch();
                                         }

                                         Output:
                                         m = 100 and n = 200
                                         There is a  difference in  output between the above two examples. The function
                                         num() is called again. The output will be 100 and 200. Here, instead of passing the
                                         values of actual parameters, the addresses of actual parameters, i.e., &m and &n are
                                         passed. The addresses are copied into formal parameters. Since the formal
                                         parameters contain the addresses, they are considered as pointers. The sequences of
                                         steps are as follows:
                                         1.  Execution of the calling process i.e., the main function will start. Since, the
                                             variables m  and  n  are  defined within the function,  memory is allocated for
                                             these variables during run-time and initialized with junk values ‘?’. Assume
                                             that the addresses of the variables are 1004 and 1008.





                          66                      LOVELY PROFESSIONAL UNIVERSITY
   68   69   70   71   72   73   74   75   76   77   78