Style 2:  void functionName(argument(s))

Our second style of function will take arguments (parameters) but will not return a value.  The argument list in the parentheses specifies the types and number of arguments that are passed to the function. 

void sum(int x, int y, int z);  //function prototype

    
Using variable names in the prototype does not actually create the variables.  It merely states the "type" of variables that will be used.

     Within the program, the function call sends actual values to the function to be processed by the function.

    sum(75, 95, 83);    //function call

 

//Example program
//Screen display shown at the right
//Passing arguments to a function

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

void greeting(int x);   //function prototype

int main(void)
{
     system("CLS");
     greeting(5);  //
function call- argument 5     
     int number;
     do
    
{
          cout<<"Please enter value(1-10):\n ";
          cin>>number;
     }
     while ((number < 1) || (number > 10));
     greeting(number);
//
argument is a variable
     return 0;
}

//function definition
void greeting(int x)   //
formal argument is x
{
     int i;   //
declaring LOCAL variable
     for(i = 0; i < x; i++)
     {
          cout<<"Hi ";
     }
     cout<<endl;
     return;  //
return value is VOID, no return
}

 

 

STOP!!

invalid function header

void sum(int x, y, z)
{ ....

 

 

Acceptable Prototypes

void add(int x, int y);

void add(int, int);

While both of these prototypes are acceptable, we will be using the first style.

 

 

Screen Display

Hi Hi Hi Hi Hi

Please enter value(1-10):
4
Hi Hi Hi Hi

 

 

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