Style 1:  void functionName(void)

Our first style of function will simply perform an independent task.  It will not send or receive any parameters and it will not return any values.  The word void appears as the return type and the parameters.

//Example program
//Screen display shown at the right
//Prototyping, defining and calling a function

#include<iostream.h>
#include<stdlib.h>

void astericks(void);   //function prototype

int main(void)
{
     system("CLS");
     cout<<"Heads up, function!\n";
     astericks( );     //function call
    
cout<<"Again, function!\n";
     astericks( );   
//function call
     cout<<"Job well done!\n";

     return 0;    //main( ) is over - ALL STOP!!
}

//function definition
void astericks(void)
{
     int count;   // declaring LOCAL variable
     for(count = 1; count<=10; count++)
          cout<<"*";

     cout<<endl;
     return;  //return value is VOID, no return
}

 

Local Variable
..a variable that is restricted to use within a function of a program.

 

SCREEN DISPLAY

Heads up, function!
**********
Again, function!
**********
Job well done!

 
 

*Note:  One of the important features of functions is their ability to be reused.  Notice how the function astericks() was called twice from main().  The programmer did not have to duplicate the code. 

 

   

 Return to Topic Menu | Computer Science Main Page | MathBits.com | Terms of Use