6.12. Functions with Empty Parameter Lists

In C++, an empty parameter list is specified by writing either void or nothing at all in parentheses. The prototype

void print();

specifies that function print does not take arguments and does not return a value. Figure 6.17 shows both ways to declare and use functions with empty parameter lists.


 1   // Fig. 6.17: fig06_17.cpp
 2   // Functions that take no arguments.
 3   #include <iostream>
 4   using namespace std;
 5
 6   void function1(); // function that takes no arguments      
 7   void function2( void ); // function that takes no arguments
 8
 9   int main()
10   {
11      function1(); // call function1 with no arguments
12      function2(); // call function2 with no arguments
13   } // end main
14
15   // function1 uses an empty parameter list to specify that
16   // the function receives no arguments
17   void function1()
18   {
19      cout << "function1 takes no arguments" << endl;
20   } // end function1
21
22   // function2 uses a void parameter list to specify that
23   // the function receives no arguments
24   void function2( void )
25   {
26      cout << "function2 also takes no arguments" << endl;
27   } // end function2


function1 takes no arguments
function2 also takes no arguments


Fig. 6.17. Functions that take no arguments.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.55.198