Pages

Wednesday, August 14, 2013

Write a program that Demonstrate Default Arguments in C++

Demonstrate use of Parameter Default

SoUrCe CoDe:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
   #include <iostream.h>
   #include <conio.h>
   using namespace std;
   // Prototype Declarations
   void printInt (int num = 0); // Note default to 0 
   int main()
   {
       // Demonstrate Default Parameter
       cout << " Calling with no Parameter \n";
       printInt ();
       // Demonstrate parameter value passed 
       cout << "\n Calling with parameter 5 \n";
       printInt (5);
       return 0;
       } //main
       /* ============= printInt =================
       Print integer value
       pre  nothing Post Either 0 or parameter 
       value printed       */
       void printInt (int num)
       {
          cout << " Parameter value is: " << num << endl; 
          getch();
          return;
                  }// printInt

OuTpUt:

 Calling with no Parameter
 Parameter value is: 0

 Calling with parameter 5 
 Parameter value is: 0

No comments:

Post a Comment