Home Segments Index Top Previous Next

101: Mainline

So far, you have seen a function definition that exhibits one parameter only. Of course, you can create functions with many parameters just as easily. When such a multiple-parameter function is entered, C pairs arguments with parameters.

Suppose, for example, that you want to define a function named trade_price that computes the total price of a stock trade, given the price per share and the number of shares traded. Now you need two, comma-separated parameters, one of which is a floating-point price and the other of which is an integer number of shares:

double trade_price (double p, int n) { 
  return p * n; 
} 

Note that you must declare the data type of each parameter individually even if all the parameters have the same type. Data types in parameter declarations, unlike data types in variable declarations, do not propagate across commas.