| barrelroll (17) | |||
I'm having a lot of trouble wrapping my brain around this snippet of code from class- I think I understand it, but when I substitute different numbers in, I am reliably wrong every time. The first time the function runs, I am OK, but the second time it prints, I'm totally lost. Here is the code:
Any help, advice, Prozac would be greatly appreciated. Thanks for taking a look. ;) | |||
|
|
|||
| hamsterman (4435) | |
|
It's okay to be confused by this. It is a horrifying mess. You could try to rewrite this without the function to understand it better. That is, instead of brainmelting(a,b,c); put the body of brainmelting, add int x = a; and replace all y or z with b or c.
| |
|
|
|
| barrelroll (17) | |
Our teacher offered it up as a way to help us learn the difference between pass by reference and pass by value- it's done nothing but confuse me, though. I can make sense of it (slowly) the first pass, but when it's passed back in... it truly melts my brain.brainmelting(b,c,a) | |
|
|
|
| Athar (4466) | |
|
What output are you expecting and why? Just execute the program on a piece of paper step by step. | |
|
|
|
| Chervil (1205) | ||
It looks to me more like an example of how not to write code. There are much tidier ways to demonstrate pass by reference and pass by value. Including global variables in the mix seems like a deliberate attempt to muddy the waters. | ||
|
|
||
| Raezzor (277) | |
|
Int x in the function is a completely new and separate variable that is initialized with the value passed to it by the function call. In the first call it is initialized with the variable a, or the value 5. In the 2nd call it is initialized by b, or the value 20. Now y and z are not new, separate variables. They are references, or sometimes called aliases. A reference is simply another name for some other variable. So in the function y and z are simply names for the 2nd and 3rd arguments passed to it. In the first call, y now refers to b and z refers to c. This is important to understand because now when you manipulate x it is ONLY modifying x, not a. However, y and z, since they are references, instead actually modify b and c (respectively.) In the 2nd call, y refers to c and z refers to a. The reason this is important is because function can only return 1 object (which includes a variable.) So, if you need to manipulate more then 1 object you can pass them by reference to the function and manipulate them indirectly. If you passed those objects by value, you would be working with copies of the objects and not the objects themselves, so the values you obtained from manipulating them would be lost once the function exits (since variables declared in a function, even in the parameter list, are destroyed once the function exits) unless you return that value (and again, you could only return a single value that way.) So any modifications you make to x in the functions is not made to the first variable you pass to the function (a in the first call, b in the second.) However, any changes made to y or z affect the globals because of them being references. | |
|
Last edited on
|
|
| barrelroll (17) | |
| Thank you. The weird thing is, I've been using functions w/pass by reference without too much trouble. The global variables were giving me hell though. I will never look at global variables the same way again. I'm pretty sure I'm never using a global variable if I can avoid it, haha. Thank you so much! | |
|
Last edited on
|
|
| Athar (4466) | ||
Of course, it's an academical example. They're often confusing on purpose - if the students cannot figure it out, it serves as a signal to them that the topics previously covered (such as scopes) haven't sunk in well enough yet. | ||
|
|
||
| Disch (8617) | |||||||
|
This is a little tricky because you are passing some variables by reference. Which means that when you change the variable in the function, those changes will be reflected outside the function. Quick example:
Your code is the exact same idea... only much more confusing because of all the math you're doing. Also, you are mixing local and global variables because you have multiple variables named 'a' and 'b'. So that's some cause for confusion. When you refer to 'a', it will chose the local variable if there is one... or the global variable if there's no local variable. So let's break this down starting with main:
Now let's step into brainmelting and see what it will do with that.
I think I got that right. I didn't actually compile this to try it out. Anyway I don't have time to walk through the second call to brainmelting but if you step through it in the same manner as illustrated above you might be able to sort it out. EDIT: bah... I'm ninja'd by like 6 people. | |||||||
|
Last edited on
|
|||||||
| barrelroll (17) | |
| ROFL, that's fine. I really appreciate everyone taking the time to look at my brain-melting code. I can't wait until I learn enough to help other people. Thank you! :) | |
|
|
|
| new cppUser (18) | |||
Maybe this might make it easier to understand (maybe):
| |||
|
|
|||
| maeriden (339) | |||
I was writing an example... good thing I refreshed the page
| |||
|
Last edited on
|
|||
| barrelroll (17) | |
| I just wanted to thank you guys again- I took my final today and a variant of this question was on it. I felt a lot better going in since I'd practiced it a bit. =) | |
|
|
|