Default Argument Function help

So simply I am new to Default Arguments and am having a bit trouble onto why this particular program is not working the way I intended to. I did it two ways, one way worked, while the other did not. For the first set of code, this program does not work, but I don't know why. I would assume the first set of number (769) would go to the variable "empNum" then the second set of number (15.75) would go to payRate to my knowledge, however it's not working for whatever reason.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

// Function prototype
void calcPay(int empNum, double payRate, double hours);

int main()
{
	calcPay(769, 15.75);

	return 0;
}

void calcPay(int empNum, double payRate, double hours = 60)
{
	double wages;

	wages = payRate * hours;
	cout << "Gross pay for employee number ";
	cout << empNum << " is " << wages << endl;

}


However this one works, it's the samething but the void calcPay function is moved above the main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

// Function prototype
void calcPay(int empNum, double payRate, double hours);

void calcPay(int empNum, double payRate, double hours = 60)
{
	double wages;

	wages = payRate * hours;
	cout << "Gross pay for employee number ";
	cout << empNum << " is " << wages << endl;

}

int main()
{
	calcPay(769, 15.75);

	return 0;
}


Also, was wondering if anyone can tell me what this means, as I did not know what it meant? Thanks a bunch.

"When an argument is left out of a function call (because it has a default value), all the
arguments that come after it must be left out too."
When I was taught prototypes in my intro class I was taught that defining parameter in the parameter list would not work, which was evident in your first case. But it worked in your second case so someone with more knowledge about function definitions before and after main can help clarify why it works. I have never seen a parameter defined in the parameter list before, so maybe it's frowned upon or I don't have a lot of experience with reading other people's code.

So the reason why the first case didn't work, it's because you defined the function with 3 parameters, but only gave it 2 (769, 15.75). One way to fix that is taking out double hours completely since you're not using it to compute anything, or defining it inside main() since you already know hours = 60 and take out the initialization double hours = 60 in your parameter list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

// Need 3 parameters
void calcPay(int empNum, double payRate, double hours);

int main()
{
      
	// calcPay(769, 15.75); has 2 parameters
        calcPay(769, 15.75);  // 3 parameters
	return 0;
}

// Took off the initialization 
void calcPay(int empNum, double payRate, double hours)
{
	double wages;

	wages = payRate * hours;
	cout << "Gross pay for employee number ";
	cout << empNum << " is " << wages << endl;

}


For the second case you can rewrite the prototype as
1
2
3
4
5
6
7
8
9
10
// Function prototype
void calcPay(int empNum, double payRate, double hours = 60)
{
	double wages;

	wages = payRate * hours;
	cout << "Gross pay for employee number ";
	cout << empNum << " is " << wages << endl;

}
Last edited on
Implementations can be in other files, other translation units. Invisible, when compiling the users.

The compiler needs to know the declarations, prototypes. That is where the default values
must be set.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

// declaration
void calcPay( int, double, double = 60 );

int main()
{
	calcPay(769, 15.75);

	return 0;
}

// implementation
void calcPay( int empNum, double payRate, double hours )
{
    using std::cout;
	double wages;

	wages = payRate * hours;
	cout << "Gross pay for employee number ";
	cout << empNum << " is " << wages << std::endl;

}


Implementation is an implicit declaration. When you have both before the main(), you essentially have:
1
2
3
4
5
6
7
8
9
void calcPay(int empNum, double payRate, double hours);
void calcPay(int empNum, double payRate, double hours = 60);

int main()
{
	calcPay(769, 15.75);

	return 0;
}

Furthermore, the compiler notices that you declare the same function twice, but manages to resolve the situation.
Awesome, thanks guys!
Topic archived. No new replies allowed.