Skip to main content

Section 5.6 Parameters

Recall that the name for the extra information a function takes when it is called are the parameters. The values we pass in when the function is called are known as the arguments. We say that the arguments are passed to the function.
int myFunction(int x, int y) {
  ...
}
Listing 5.6.1. myFunction has two parameters, x and y.
int main() {
    int result = myFunction(2, 4);
}
Listing 5.6.2. The arguments 2, 4 are being passed to myFunction in a call made from main.

Checkpoint 5.6.1.

A function can have no parameters, one parameter, or many parameters. Each parameter must specify a type and multiple parameters must be separated by commas.
Valid parameter lists:
Invalid parameter lists:
  • int foo { (missing the parameter list)
  • int foo(int x, y) { (no type for y)
  • int foo(int x double y) { (no comma between x and y)

Checkpoint 5.6.2.

Which of the following is a correct function header (first line of a function definition)?
  • totalcost (double cost, tax, discount)
  • totalcost needs a return type, and each parameter needs a data type.
  • totalCost (double cost, double tax) {
  • totalcost needs a return type.
  • void totalCost (double cost, double tax, double discount) {
  • Correct!
You have attempted of activities on this page.