Page 79 - Modular_V1.1_Flipbook
P. 79

cout<<”The table of the entered number is: \n”;                           Output:
                      table(num);                     // num1 is actual arguments               Enter a number: 10
                      getch();                                                                  The table of the entered
                 }                                                                              number is:
                                                                                                10 × 1 = 10
                 void table(int n)                    //  n is formal parameter
                                                                                                10 × 2 = 20
                 {
                                                                                                10 × 3 = 30
                      int i = 1;
                                                                                                10 × 4 = 40
                      while(i<=10)
                                                                                                10 × 5 = 50
                      {
                                                                                                10 × 6 = 60
                      cout<<n<<” x “<< i <<” = “ <<n*i<<”\n”;
                                                                                                10 × 7 = 70
                      i++;
                                                                                                10 × 8 = 80
                      }
                                                                                                10 × 9 = 90
                 }                                                                              10 × 10 = 100

                     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>                                                  Output:
                                                                                    The  values  before  swapping  are:
                 void swap(int num1, int num2);
                                                                                    10 and 20
                 void main()
                                                                                    The values after swapping are: 20
                 {
                                                                                    and 10
                     int a = 10, b = 20;                                            The values of and b are: 10 and 20
                     clrscr();
                     cout<<”The values before swapping are: “<<a<<” and “<<b<<”\n”;
                     swap(a, b);
                     cout<<”The values of and b are: “<<a<<” and “<<b<<”\n”;
                     getch();
                 }
                 void swap(int num1, int num2)
                 {

                     int temp;
                     temp = num1;
                     num1 = num2;
                     num2 = temp;
                     cout<<”The values after swapping are: “<<num1<<” and “<<num2 <<”\n”;
                 }


                                                                                               Functions in C++   77
   74   75   76   77   78   79   80   81   82   83   84