Jump to content

C++ void function question

Kagami Tsukino

I do not understand this question that my teacher asking. Does anyone know what it mean?

 

Given the following function definition  

void calc (int a, int &b) 

{   

   int c;    

   c = a +5;    

   a = a * 2;   

   b = c + a + 2; 

}  

What is the output of the following code fragment that invokes calc

 

 int x = 1; 

 int y = 2; 

 int z = 3;  

 calc (x, y); 

 cout << x << “ “ << y << “ “ << z << endl;

Explain why the statement calc(1, 2); would be an error.

Link to comment
Share on other sites

Link to post
Share on other sites

Probably because in calc, b is a memory address.

 

Edit: What is the purpose of this anyway since nothing actually gets done (nothing is printed or returned) also what error does calc give? 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, djdwosk97 said:

Probably because in calc, b is a memory address.

 

Edit: What is the purpose of this anyway since nothing actually gets done (nothing is printed or returned) also what error does calc give? 

It's to learn about references, it doesn't have an actual point. 

 

The second example is an error because an rvalue (2) can't bind to a non const reference.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, fizzlesticks said:

It's to learn about references, it doesn't have an actual point. 

 

The second example is an error because an rvalue (2) can't bind to a non const reference.

I really hate how professors teach with abstract references. I've learned so much more from the coding assignments where I know what I'm doing and not just doing something random/abstract and following a doc or something similar. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, djdwosk97 said:

I really hate how professors teach with abstract references. I've learned so much more from the coding assignments where I know what I'm doing and not just doing something random/abstract and following a doc or something similar. 

me too it already confusing but this is so mess up. I can't even figure how this program even work or the display would be. 

*What is the output of the following code fragment that invokes calc?

 

here an example but I do not know how it got this number

 

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Kagami Tsukino said:

me too it already confusing but this is so mess up. I can't even figure how this program even work or the display would be. 

*What is the output of the following code fragment that invokes calc?

 

here an example but I do not know how it got this number

 

oooo, I see what it's doing. 

 

Okay, so you're passing in x (1) and the memory address of y (the location that stores 2) into calc. Then in calc, you set c = 3, a = 3, b = 6.

 

So when you print in your main function, what you're doing is printing: X (1), then y (which was changed in the calc function when you changed the value of b), and then Z (3). The reason Y changes from 2 to 6 but X remains unchanged is because calc uses the address of b (which is the same address as Y -- so when you go to print Y you're accessing the same memory that calc modified), however with a, calc just modified the local variable and not the memory location (so it doesn't persist when you go to print x).

 

Edit: I feel like I butchered that explanation. If you're confused by it let me know which part and I'll try to be more clear. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Kagami Tsukino said:

<snip>

void calc (int a, int &b) 
{   
	int c;    
	c = a +5;    
	a = a * 2;   
	b = c + a + 2; 
}  

In the above function definition:

-Parameter 'a' is called by value - that means it is a copy of the original parameter passed to the function when it is called. Any changes made to 'a' will not affect the original parameter passed to the function.

 

-Parameter 'b' is called by reference (because of the & before the name) - that means it refers to the original parameter that was passed to the function when it was called. Any changes made to it will affect the original parameter that was passed to the function.

 

This already explains the question:

Quote

Explain why the statement calc(1, 2); would be an error.

Simply put: Because when the function tries to modify parameter 'b', it would be trying to modify a constant (2), which is impossible, it would be the same as:

2 = c + a + 2; 
 

Which obviously makes no sense as a programming statement. Read up on rvalues and lvalues for more details.

 

As for the output:

void calc (int a, int &b) 
{   
  	//After the call, a == 1 and b == 2
  
	int c;    
	c = a +5;    // c == 6
	a = a * 2;   // a == 2
	b = c + a + 2; //b = 6 + 2 + 2
}  

	int x = 1; 
	int y = 2; 
	int z = 3;  
	calc (x, y); 
	cout << x <<   << y <<   << z << endl; 

//x will always remain 1 because the function cannot modify it, because it is called by value
//z will always remain 3 because it was assigned 3 and never used thereafter, only to print
//y will be 10, because the function can modify it because it was called by reference and the
// result of the operations in the function is 10

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

40 minutes ago, Unimportant said:

This already explains the question:

Simply put: Because when the function tries to modify parameter 'b', it would be trying to modify a constant (2), which is impossible, it would be the same as:


2 = c + a + 2; 
 

Which obviously makes no sense as a programming statement. Read up on rvalues and lvalues for more details.

It shouldn't always throw an error, it should just print out a number that doesn't make sense. :)

Also, your professor is a bit weird. I'd rather he gave you an example of how you can use, see, and understand a pass by reference. How each compiler handles these kinds of things are very different. Teaching something that depends on the compiler/operating system doesn't really make sense to me.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Steven Horton said:

It shouldn't always throw an error, it should just print out a number that doesn't make sense. :)

No, it won't even compile, you can't run something that does not compile, and something that can't run can't print nonsense.

 

It will always give you a compiler error. Something along the lines of not beeing able to assign to a rvalue or invalid initialization of parameter with a rvalue.

 

If 'b' were a const reference, then the function call would be ok, as you can use constants for const references, obviously.

But then the error would move to the line:

b = c + a + 2;

 

as you'd be trying to assign to/modify a const reference.

 

His professor is not weird, what he's trying to show is not even about C++, its basic common sense.

He's showing that you cannot do something like:

2 = 10

Which is essentially what the whole thing boils down to. And every compiler in any language will trip over that. Understanding how you arrive at this impossible statement trough a detour of a function call and references is the point of the exercise.

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/14/2016 at 5:46 AM, Unimportant said:

It will always give you a compiler error.

I believe @Steven Horton was referring to the calc function itself not always throwing an error, for example how calc(a, b) is valid while calc(1, 2) is not.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Yamoto42 said:

I believe @Steven Horton was referring to the calc function itself not always throwing an error, for example how calc(a, b) is valid while calc(1, 2) is not.

 

 

That would be true, however in that case it will never print nonsense as he claimed, but work as planned every time.

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/15/2016 at 7:46 AM, Unimportant said:

That would be true, however in that case it will never print nonsense as he claimed, but work as planned every time.

Well, thank you for being civil -- always cool. If I remember correctly, when I tested it, it said that y was 12. :) Which, in my opinion, is nonsense. (insert common core joke here)

Edited by Steven Horton
Link to comment
Share on other sites

Link to post
Share on other sites

On 12/14/2016 at 5:46 AM, Unimportant said:

His professor is not weird, what he's trying to show is not even about C++, its basic common sense.

He's showing that you cannot do something like:


2 = 10

Which is essentially what the whole thing boils down to. And every compiler in any language will trip over that. Understanding how you arrive at this impossible statement trough a detour of a function call and references is the point of the exercise.

Except maybe Matlab. Matlab silently lets you overwrite the imaginary number "i" if you use it in a for loop, lol

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×