Function issues

I am having some issues with functions and I have looked at a lot of websites that try to explain it however I just am not getting it. Mine is having issue and I know its something that I missed on using them. Any help will be appreciated.

This here is the prototype above main
 
float regpay (float , float);



This is where I want the Function to display
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
switch (choice)
{
	case 1:
	{
	        system("cls");
		cout << "You selected to see " << lastname << " " << firstname << " Regular pay";
		cout << "\n\n" << regpay;
		cout << "\n\n\nWould you like to display another? (y or n) ";
		cin >> answer;
					
		if ((answer == 'y') or (answer == 'Y'))
			{
				again = true;
			}
			else
			{
				again = false;
			}
					
			break;
		}
				
	}


And here is where I am trying to define the function. I think the issue is in this area however, I am unsure of what it is. Maybe there is a local variable that I need to use?

1
2
3
4
5
float regpay (float grosspay, float hours)
{
		regpay = grosspay * hours;
		return regpay;
}		



Thanks for any help you can give me. I didn't want to post the whole code simply because I know that the function part of the code is the issue. Thanks again.
Several things:

1.) The prototype and the function definition don't match. They don't match because in your definition, you gave identifier names to the float parameters, whereas in the declaration/prototype, you didn't.

2.) Line 3 of the regpay() function snippet is attempting to perform an assignment to a variable that doesn't exist. Fix this by doing either:

a.)
1
2
3
4
float regpay(float grosspay, float hours) {
	float regpay = grosspay * hours;
	return regpay;
}


or

b.)
1
2
3
float regpay(float grosspay, float hours) {
	return grosspay * hours;
}


3.) You're not actually calling the regpay() function on line 7 of your printing snippet.

You would call it like this:

std::cout << regpay(grosspay, hours) << std::endl;
Where grosspay and hours are the respective parameters.
Last edited on
Hey xismn thanks for your reply but I still feel a little confused. Here is what I think I know about functions.

The prototype or :
 
float regpay (float , float);


says that this function is called regpay and that it will recieve a float and a float and it will return a float.

Then in the main of the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
switch (choice)
{
	case 1:
	{
	        system("cls");
		cout << "You selected to see " << lastname << " " << firstname << " Regular pay";
		cout << "\n\n" << regpay;
		cout << "\n\n\nWould you like to display another? (y or n) ";
		cin >> answer;
					
		if ((answer == 'y') or (answer == 'Y'))
			{
				again = true;
			}
			else
			{
				again = false;
			}
					
			break;
		}
				
	}


where ever I put the 'regpay' is where I want it to jump down into the function below and do what it says.



Then I define the function as:
1
2
3
4
5
6
float regpay (float grosspay, float hours)
{
		regpay = grosspay * hours;
		return regpay;
}		


So therefore the return is what should return in the "regpay" in the main. Is this correct or am I missing something?
You aren't quite there yet.

Here is how you call any function:

1
2
3
4
5
6
7
8
9
void function() {//this is just the declaration / definition
	/*do something*/
}

int main() {
	//this is where you call the function
	function();//notice the parentheses
	return 0;
}



Similarly, if I have this:

1
2
3
int return_five() {
	return 5;
}


And I want to print the value that is returned by this function (namely five), then I would do this:

1
2
3
4
5
6
int main() {

	std::cout << return_five() << std::endl;//again, note the parentheses

	return 0;
}


If I didn't have the parentheses, the compiler would assume that I'm trying to print a variable with the same name.

1
2
3
4
5
6
int main() {

	std::cout << return_five << std::endl;//error, there is no identifier "return_five"

	return 0;
}



If your function expects arguements / parameters, then you need to supply them to the function when you are calling it.

Let's say I have this function, which adds two integers together, and returns the sum:

1
2
3
int sum(int a, int b) {
	return a + b;
}


I need to give it two parameters when I call it. It wouldn't make sense to call a function that adds two numbers together, and then to only give it one number.

1
2
3
4
5
6
int main() {

	std::cout << sum(1, 2) << std::endl;//prints the sum - three in this case

	return 0;
}



One final thing. This has to do with scope.
In C++, most of everything is stack-bound (unless you're dealing with dynamic memory, I won't get into that). When I say "stack-bound", I mean that things like variables only exist within a certain scope (area) of your code, and if you attempt to access them anywhere else, it's very likely that you'll get a compilation error.

To illustrate this better, here I have a main function:

1
2
3
4
5
int main() {
	int a = 10;

	return 0;
}


Which doesn't really do anything exciting. It does, however, instantiate an integer by the name of "a", and gives it a value of ten.

Once the main function terminates, that integer dies, and ceases to exist. You might say "Duh, because that's when the program ends". This is true. Perhaps a better example would be this:

1
2
3
4
5
6
7
8
9
10
11
void function() {
	int a = 10;
}

int main() {
	function();

	a = 20;

	return 0;
}


What do you think this code does? If you answered : "It will change the value of "a" to twenty", then you're wrong. It shouldn't compile. The reason is because the integer "a" only exists within the scope of void function(). The main function has no idea what "a" is.

Therefore, your snippet:

1
2
3
4
5
float regpay (float grosspay, float hours)
{
		regpay = grosspay * hours;
		return regpay;
}


Is incorrect, and shouldn't compile. In the regpay() function, there is no regpay variable. Therefore, this should result in an undeclared identifier error.

I once summed up the concept of scope a lot nicer in another thread - I'm trying to find it right now. I'll edit this post once I find it.

As far as mismatched function prototypes and definitions go, they need to match 100%. Here's how the regpay() prototype should look:

float regpay(float grosspay, float hours);
And before anyone goes correcting me, yes I realize there are unnamed / anonymous parameters. OP should not be concerned with those ATM.
Last edited on
xismn,
Thanks for your reply. I have been studying what you had to say for a while. Its hard to decipher what you are trying to say. I don't think that you are wrong I was shown to do it a different way and its hard to learn two ways at one time. I am going to sit here and trying to figure it out. Thank you for helping.
Topic archived. No new replies allowed.