This site is no longer active and is available for archival purposes only. Registration and login is disabled.

C++


Re: C++

Postby Warren » Apr 17, 2001 @ 4:16am

Ok, I've been learing C++ for a few days now, and this one thing is driving me insane. What is this "passing by value" thing?
Warren
pm Insider
 
Posts: 3140
Joined: Mar 13, 2001 @ 5:49pm


Re: C++

Postby randall » Apr 17, 2001 @ 4:43am

Passing by value makes a temporary copy of the variable/parameter- allowing the original variable to remain unchangable by the called function.<br><br>Okay that was confusing. Let's try an anology.<br><br>By Value- Your friend asks what your girlfriend looks like, so you show him a picture. He can see what she looks like, but has no real interaction with her directly.<br><br>By Reference- Your friend asks what your girlfriend looks like, so you give him her street address and phone number so he can go see for himself. Now he has direct interaction with her.<br><br>Often you just need to "use" a variable, rather than modify it.Last modification: randall - 04/17/01 at 01:43:28
User avatar
randall
pm Insider
 
Posts: 3426
Joined: Feb 23, 2001 @ 4:02pm
Location: Schnoogie


Re: C++

Postby Moose or Chuck » Apr 17, 2001 @ 4:43am

Moose or Chuck
 


Re: C++

Postby randall » Apr 17, 2001 @ 4:47am

User avatar
randall
pm Insider
 
Posts: 3426
Joined: Feb 23, 2001 @ 4:02pm
Location: Schnoogie


Re: C++

Postby randall » Apr 17, 2001 @ 4:50am

User avatar
randall
pm Insider
 
Posts: 3426
Joined: Feb 23, 2001 @ 4:02pm
Location: Schnoogie


Re: C++

Postby Warren » Apr 17, 2001 @ 5:15am

Warren
pm Insider
 
Posts: 3140
Joined: Mar 13, 2001 @ 5:49pm


Re: C++

Postby Moose or Chuck » Apr 17, 2001 @ 5:30am

Moose or Chuck
 


Re: C++

Postby randall » Apr 17, 2001 @ 5:40am

Oops, Moose sneaked one in before me. This is in reference to Warrens post preceding Moose:<br><br>Yeah, that would be passing by value.<br><br>Notice how the original X/Y variables weren't changed by the function? This is because the function was only working with a copy. Kind of like scribbling on a photograph of the Mona Lisa- it has no effect on the original residing in the Louvre'.<br><br>Now if these were passed by reference, the originals would have been swapped.<br><br><br>Last modification: randall - 04/17/01 at 02:40:02
User avatar
randall
pm Insider
 
Posts: 3426
Joined: Feb 23, 2001 @ 4:02pm
Location: Schnoogie


Re: C++

Postby Dan East » Apr 17, 2001 @ 7:01am

Warren wrote:<br>int x = 3 <br>int y = x * 3 <br>y = 9 <br>x = 3 <br><br>Is that it? <br><br><br>Nope. As the other guys have said, this involves calling a function. A basic way of describing it is if a function is written to accept a parameter by reference, then that function can modify that variable's value. When passing by value the function cannot modify that variable's value (outside the scope of the function). I don't know if this will help you or confuse you more, but another technique that achieves the same result as passing by reference is to pass a pointer to the variable. Since the function has the pointer to the variable it can modify the variable's value:<br><br>Sample passing a pointer to a variable<br><br>void add(int *result, int a, int b) {<br>//Result is a pointer to an integer variable<br><br>*result=a+b;<br>}<br><br>int main() {<br><br>int result;<br><br>//We send "add" a pointer to "result" variable.<br>add(&result, 10, 20);<br><br>//The add function modified result<br>//so it is now equal to 30<br>}<br><br><br>Sample passing by reference<br><br>void add(int &result, int a, int b) {<br>result=a+b;<br>}<br><br>int main() {<br><br>int result;<br><br>add(result, 10, 20);<br><br>//The add function modified result<br>//so it is now equal to 30<br>}<br><br>Notice that passing by reference is simply a more elegant and less confusing method of passing pointers to variables. Most functions do not modify the variables that were passed to them, hence they aren't written to accept variables by reference. Note also that when you call the function you do not have to do anything different to pass by reference or pass by value.<br><br>Dan East
User avatar
Dan East
Site Admin
 
Posts: 5264
Joined: Jan 25, 2001 @ 5:19pm
Location: Virginia, USA


Re: C++

Postby Warren » Apr 17, 2001 @ 11:13am

Thanks Dan, now I get it, it's pretty much a reference to variable(s) outside the function, without changing the value of the variable in the previous function. Or something like this, you know, I'll learn more about it as I progess.
Warren
pm Insider
 
Posts: 3140
Joined: Mar 13, 2001 @ 5:49pm


Re: C++

Postby Paul » Jun 19, 2001 @ 4:48am

just a quick question - whats a good book for learning ms visual c++?
Paul
pm Insider
 
Posts: 9835
Joined: Apr 2, 2001 @ 3:15pm
Location: California


Re: C++

Postby jongjungbu » Jun 19, 2001 @ 12:43pm

There isn't any good book for learning MS Visual C++ IMHO because they all want you to use MFC. Bleh!<br><br>
User avatar
jongjungbu
Not JongJongBu
 
Posts: 3112
Joined: Jun 19, 2001 @ 4:22am
Location: USA


Re: C++

Postby Paul » Jun 19, 2001 @ 2:00pm

shut up newbie
Paul
pm Insider
 
Posts: 9835
Joined: Apr 2, 2001 @ 3:15pm
Location: California


Re: C++

Postby jongjungbu » Jun 19, 2001 @ 2:27pm

:P<br>then get Using Visual C++ 6 by Jon Bates and Tim Tompkins. I find it to be pretty good. Better for developing apps (MFC-style) then games. The last DirectX book i bought was DirectX Complete by Michael Root & James Boer which covers DX at the time of DX6 i think. Not that much has changed in the way of fxn's. So there's yer freakin answer then Paul-ie Shore.<br>Zesteh!<br><br>
User avatar
jongjungbu
Not JongJongBu
 
Posts: 3112
Joined: Jun 19, 2001 @ 4:22am
Location: USA


Re: C++

Postby W3bD£v1l » Jun 19, 2001 @ 2:30pm

I'd just get the full Microsoft Training guide, go on a few courses and seminars. Hey, you can get your employer to fund you, (technically its training)
W3bD£v1l
pm Member
 
Posts: 293
Joined: Apr 19, 2001 @ 1:35am


PreviousNext

Return to Windows Mobile


Sort


Forum Description

A discussion forum for mobile device developers on the Windows Mobile platform. Any platform specific topics are welcome.

Moderators:

Dan East, sponge, Digby, David Horn, Kevin Gelso, RICoder

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum