Page 91 - Modular_V2.0_C++_Flikpbook
P. 91

cout<< "Enter a number: ";
                     cin>> num;
                     cout<< "The table of the entered number is: \n";
                     table(num);
                                                                            DOSBox 0.74, Cpu speed: max 100% cycles, Frameskip 0,
                     getch();
                                                                         Enter a number: 9
                 }                                                       The table  of the entered  number
                 void table(int n)                                       is:
                 {                                                       9 × 1 = 9
                                                                         9 × 2 = 18
                     int i = 1;
                                                                         9 × 3 = 27
                     while(i <= 10)                                      9 × 4 = 36
                     {                                                   9 × 5 = 45
                                                                         9 × 6 = 54
                          cout<< n << " x " << i << " =
                                                                         9 × 7 = 63
                            " << n * i << "\n";
                                                                         9 × 8 = 72
                         i++;                                            9 × 9 = 81
                     }                                                   9 × 10 = 90
                 }

                    DIFFERENT METHODS TO CALL A FUNCTION

                 There are two methods to call a function: call by value and call by reference.
                 Call by Value


                 Call by value is a technique of passing the values of variables instead of actual variables to
                 a function while calling it. It is a default method of C++ to call a function. In this technique,
                 changes made to the parameters inside the function have no effect on the arguments.
                 Program 3: To swap two numbers by using call by value method of function call.

                 #include<iostream.h>
                 #include<conio.h>
                 void swap(int num1, int num2);
                 void main()
                 {
                     int a = 10, b = 20;
                     clrscr();
                      cout<< "The values before swapping are: " << a << " and " << b <<
                      "\n";
                     swap(a, b);  // Function call (Call by Value)
                      cout<< "The values of a and b after function call: " << a << " and
                      " << b << "\n";
                                                                    DOSBox 0.74, Cpu speed: max 100% cycles, Frameskip 0,
                     getch();
                                                                 The value before swapping are: 10 and 20
                 }
                                                                 The values inside the swap function after
                                                                 swapping are: 20 and 10
                 void swap(int num1, int num2)                   The value of a and b after function call:
                                                                 10 and 20
                 {
                                                                                                                  89
                                                                                                Functions in C++
   86   87   88   89   90   91   92   93   94   95   96