Click to See Complete Forum and Search --> : I'm a terrible programmer
JoniMitchellRockedMyWorld
10-07-2000, 09:41 PM
I'm learning about functions in C right now and they're kinda confusing me. The reasoning behind them makes perfect sense to me. It's just the part about passing by values, passing by address, and returning values that's kinda messing with my head. How sad is that?
How sad is that?
Not at all; trust me, in time, they'll be second nature. Just working with them will help you figure it out. If something doesn't seem to be working the way you thought it did, try something different and pay attention to what happens. Don't worry, if you want to program and you like to program, then in time, you'll still be a terrible programmer. Just on a different level. http://www.linuxnewbie.org/ubb/biggrin.gif That's one of the reasons I love this game.
------------------
-Pacotron- You are what you edit- vi -------- GAIM: KMJ2L
Don't be left out! STAND UP AN BE COUNTED! (http://www.cs.rit.edu/~kmj9907/cgi-bin/pollster.cgi)
--*real men use man*--
-The only reason I keep my DOS partition is so I can mount it like the b*tch that it is.
[This message has been edited by kmj (edited 07 October 2000).]
Serbot
10-07-2000, 09:51 PM
Look at it this way:
You'll never be worse than me
http://www.linuxnewbie.org/ubb/smile.gif
Strike
10-08-2000, 03:00 AM
Not that you asked, but I'll give a mini-explanation anyway.
Remember that any variable you declare in a program has to be stored somewhere in memory, right? Well, the way memory is accessed is kind of like a big array, and each little bit has a sort of "index". Well, indices in memory are called "addresses" instead. So, each variable has an address assigned to it, and that is how programs access that variable.
What does this have to do with passing by value and passing by reference/address? Well, when you call a function, one of two things happens. To explain them, let's come up with a sample program:
#include <stdio.h>
void myfunction(int a, int *b);
main ()
{
int a = 3;
int b = 6;
printf("Before the call...\n");
printf("a = %d\n", a);
printf("b = %d\n", b);
myfunction(a, &b);
printf("After the call...\n");
printf("a = %d\n", a);
printf("b = %d\n", b);
}
void myfunction(int a, int *b)
{
printf("\tIn the function, before assignments...\n");
printf("\ta = %d\n", a);
printf("\tb = %d\n", *b);
a = 100;
*b = 500;
printf("\tIn the function, after assignments...\n");
printf("\ta = %d\n", a);
printf("\tb = %d\n", *b);
}
As you may have noticed, if you compile and run this code, that you get this output:
Before the call...
a = 3
b = 6
In the function, before assignments...
a = 3
b = 6
In the function, after assignments...
a = 300
b = 600
After the call...
a = 3
b = 600
You see, the variable b was passed by reference (I use this instead of "address"), and a was passed by value.
When I called myfunction(a, &b), what I really sent was:
a copy of the value located at a's storage location in memory
the address of b's storage location in memory
So why didn't a change? Well, if the function doesn't know where a is stored in memory, it can't change the value stored there. It can use the value contained at that location, but it has no idea where that value came from. And why did b change? Well, the function was sent b's address in memory, so it knew how to. And you may have noticed that I didn't refer to b as just b in that function, but *b. What that notation means is "the variable pointed to by the address of b". If I did just b I would actually be changing the address that was passed in, resulting in no change of the variable b and weird numbers when trying to use b for anything useful.
Okay, so this wasn't so "mini" an explanation, but hopefully it was helpful http://www.linuxnewbie.org/ubb/smile.gif
JoniMitchellRockedMyWorld
10-08-2000, 03:12 AM
Strike - Thanks http://www.linuxnewbie.org/ubb/smile.gif Your explanation cleared up a few things for me....much appreciated
binaryDigit
10-08-2000, 08:41 AM
Strike:
where were you when i needed you.
that is the best explanation i've seen yet.
and to think of all the time i spent reading and re-reading.
that particular subject had me confused for a long time.
http://www.linuxnewbie.org/ubb/smile.gif
------------------
http://home.earthlink.net/~pebice/philLinux.html (http://home.earthlink.net/~pebice)
Strike
10-08-2000, 01:21 PM
Cool, glad to help.. and binaryDigit, I don't remember hearing anyone ask this before ... but then again, I miss posts here and there. What can I say? http://www.linuxnewbie.org/ubb/tongue.gif
binaryDigit
10-08-2000, 02:26 PM
Strike:
i never posted a question about that one because i wanted to figure it out myself.
i just wish your explanation had been in one of the books i have. things would have been much easier. http://www.linuxnewbie.org/ubb/smile.gif
------------------
http://home.earthlink.net/~pebice/philLinux.html (http://home.earthlink.net/~pebice)
TheLinuxDuck
10-10-2000, 10:20 PM
Originally posted by JoniMitchellRockedMyWorld:
Strike - Thanks http://www.linuxnewbie.org/ubb/smile.gif Your explanation cleared up a few things for me....much appreciated
Joni, there is always someone here who is willing to take the time to offer some help and aide, when needed. Strike's a fart smeller.. I mean a smart feller, and can surely help.. http://www.linuxnewbie.org/ubb/smile.gif
If you have any other questions or are confused about anything, please don't hesitate to post. http://www.linuxnewbie.org/ubb/smile.gif
The same goes for you binaryDigit!!! http://www.linuxnewbie.org/ubb/smile.gif
------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq
JoniMitchellRockedMyWorld
10-10-2000, 11:21 PM
Strike's a fart smeller.. I mean a smart feller, and can surely help..
LOL http://www.linuxnewbie.org/ubb/smile.gif
BTW thanks for the offer . I'm sure I'll take you up on it sooner or later.