Default parameters and recompiling

Hello

I just had a discussion with a colleague at work who is responsible for the building of our rather large software product. To reduce file size, time used, and general hassles, he wants to rebuild as little code as possible when we make code changes. In this case, I had made a code change like so:

void Func1(int a); // Original
void Func1(int a, int b = 0); // New

Func1 is already being used at a lot of places in client code, and I didn't want to change that code in umphteen places, hence the default parameter. My code resides in a dll, and the client code resides in other dlls (not that I think that has much bearing on the answer to my question).

Now, I say that even though there is a default parameter, the client code, while not needing to be changed, must nevertheless be recompiled with my code's updated .h file, because the only thing that the default parameter does, is having the compiler quietly insert the default parameter value as parameter 2 at every call to Func1. The binary Func1 doesn't even know about its own default value; it expects 2 parameters of type int, and that's it.

My colleague begs to differ, thinking that the function knows that it has a default value, and uses that if the client passes only one parameter.

Who is right (or are we both wrong, and there is a 3rd answer)?
Good question. Here's how I double-checked before answering :)

1) Compiled the following into an object file (i.e. a simple library):

1
2
3
4
5
6
7
#include <iostream>
using namespace std;

void giveMeANumber ( int x)
{
  cout << x;
}


2) Wrote the following, compiled and linked it with the above already-compiled object:

1
2
3
4
5
6
7
void giveMeANumber(int x);

int main()
{
  int x;
   giveMeANumber();
}


3) Ran the exectuable. The output was some random number, as expected.

4) Changed the second set of code but DID NOT touch the first:

1
2
3
4
5
6
7
void giveMeANumber(int x = 65);

int main()
{
  int x;
   giveMeANumber();
 }

It now has a default parameter value, but the actual function was written and compiled without any such thing, and still exists untouched in the object file.

Ran the new code; output was 65.

Clearly, in this implementation, the default value is silently inserted before calling the function from the library.
Last edited on
Very interesting. The only thing that surprises me is that you get to compile

giveMeANumber();

the first time.

Doesn't the compiler complain about the missing argument before you provide the default parameter?
Nuts. Cut n' paste error! Sorry. The first time I fed it the x value;
 
giveMeANumber(x);
Topic archived. No new replies allowed.