Lab 8: Experiment 4

Number of Parameters ≠ Number of Arguments

Make sure you undid the modifications to your code from the previous exercise!

In this experiment, we will address two issues.

Issue A: What happens if the number of arguments in a function call does not match the number of parameters in the function prototype?

Hypothesis: The number of parameters in a function prototype and the number of arguments in a function call does matter; they should be the same.

The Experiment: In the prototype of function change(), add a fourth parameter so that the number of parameters differs from the number of arguments.

    void change(int param1, int param2, int param3, int param4)

Question #8.4.1: What will the compiler do if it doesn't like this?

Observation: Compile your program.

Question #8.4.2: What happens? Be specific.

Conclusions:

Question #8.4.3: Is our hypothesis correct? If it's not correct, what should it be?

Restore the prototype to its original form, and then continue.


Issue B: What happens if the number of arguments does not match the number of parameters in the function definition?

Hypothesis: The number of parameters in a function definition and the number of arguments in a function call does matter; again, these two numbers must be the same.

The Experiment:

In the definition of function change(), add a fourth parameter so that the number of parameters differs from the number of arguments:

    void change(int param1, int param2, int param3, int param4)
    {
      param1 = 1;
      param2 = 2;
      param3 = 3;
    }

Don't change the prototype for this function; it should have only three parameters.

Observation: Compile your program.

Question #8.4.4: What happens? Be specific.

Question #8.4.5: How is this result different from the results of changing the prototype? They are different, and understanding this difference will help you in debugging code.

Conclusions:

Question #8.4.6: Is our hypothesis correct? If it's not correct, what should it be?

Restore the definition to its original form and then continue.


Back to the Lab Exercise  |  Forward to the Next Experiment


Report errors to Larry Nyhoff (nyhl@cs.calvin.edu)