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


void greeting(int x);   //function prototype

int main(void)
     greeting(5);  //
function call- argument 5     
     int number;
          cout<<"Please enter value(1-10):\n ";
     while ((number < 1) || (number > 10));
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 ";
     return;  //
return value is VOID, no return




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):
Hi Hi Hi Hi



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