void calcFatInfo(gibberish)

K.I.L.E.R

Retarded moron
Veteran
So what's up with function prototypes? I never used them. I never had a need. So what do they do?

The reason I am asking is because I just went to court (court order against father) and missed class but I did ask the teacher about what I have to catch up on. The book doesn't explain anything about it.

Thanks
 
Prototypes let you call a function before you define it. You're basically telling the compiler:

"there's going to be a function of this type, with this name, and these parameters, that is defined later on, so keep that in mind and don't bitch to me when I make calls to this function in the code before that"

Prototypes generally either go at the top of a source code file, or in the header file. If you don't have multiple source files, and you arrange your code so that functions are never called before they're defined, you don't need to use prototypes, but even then it's nice to put them at the top of the file so someone else can easily see what functions you're going to define.
 
Thanks. Nice and easily explained too. :)
Double thanks for that.

One other question:
void calcFatInfo(int, int, int &, float &);

WTF is up with using 1 amplasan?
&& = and
& = ?

Thanks
 
That's a fairly long and difficult to grasp concept to explain here. Check in your book for "pass by reference" or "call by reference" parameters for further explanation if this isn't sufficient.

The short answer is that using the ampersand causes the function to receive the memory address of the variable, instead of the contents of the variable. Example:

Code:
// prototype
void foo (int v, int& r);

void main() {
int x = 5;
foo (x, x);
}

The value of x is 5. When you pass x as the parameters to foo(), it creates a variable called v and assigns it the value of 5 (what it receives as the first parameter). For the second parameter, foo() actually receives a pointer, or a memory address, instead of the value of the variable that was passed to it. It assigns the variable r to the same memory address that x points to, instead of making a new variable and giving it the same value as x. If you change the value of v inside of foo(), nothing happens to x. However, anything that you do to r is also done to x, since they're really referring to the same space in memory.
 
// prototype
void foo (int v, int& r);

void main() {
int x = 5;
foo (x(v), x(r)); //because of the '&' in [int r] we can change r's value anywhere in the code and it will change the value of 'x' and in turn changing v?
}



So I can use this method to solve for a variable in a long equation?

Thanks
 
v is a completely independent variable. The only thing it has in common with x is that it was given the same value that x had when the function was called. Changing x or r will have no affect on v, and vice versa. X and r can be thought of as two different names for the exact same variable (variable = chunk of memory representing some value).
 
Ah, so if 'v' had an ampersand in the declared int (the one in the void), it would then change if the value of 'x' changed, otherwise it won't?
Same goes for every other variable.

That single ampersand dictates which variables share the same memory addy?
 
K.I.L.E.R said:
Ah, so if 'v' had an ampersand in the declared int (the one in the void), it would then change if the value of 'x' changed, otherwise it won't?

Well, don't get confused here... x,v, and r are local variables... they only exist in the functions they are created in. If you try to do something to x inside of foo(), you'll get an error, because when you're in foo() the computer doesn't know a variable named x even exists, let alone what memory address it has. Likewise, if you try to access v anytime you aren't in foo(), you'll get an error, because v only exists inside of foo().

But, if both v and r were pass-by-reference (both had the ampersand after the parameter type in the foo() function definition, e.g. void foo(int& v, int& r) ), then inside of foo(), changing v wouldaffect the contents of r and x, since they would all be the same piece of memory.
 
Crusher said:
K.I.L.E.R said:
Ah, so if 'v' had an ampersand in the declared int (the one in the void), it would then change if the value of 'x' changed, otherwise it won't?
Same goes for every other variable.

That single ampersand dictates which variables share the same memory addy?

Correct

THANK YOU! :)
Thank you very much. Now I just have to finish off my work and then I am done. :)
 
K.I.L.E.R said:
Crusher said:
K.I.L.E.R said:
Ah, so if 'v' had an ampersand in the declared int (the one in the void), it would then change if the value of 'x' changed, otherwise it won't?
Same goes for every other variable.

That single ampersand dictates which variables share the same memory addy?

Correct

THANK YOU! :)
Thank you very much. Now I just have to finish off my work and then I am done. :)

Sorry, read your post a little too quick, read my edited response please :) The last part of your question is correct, though:

That single ampersand dictates which variables share the same memory addy?
That's true
 
K.I.L.E.R said:
Ah, thanks.
So only locals are allowed to change, foriegners are not. ;) (joke) :LOL:

No... not exactly... local variables are only allowed to be accessed directly within the function they're defined in. They can still be changed elsewhere, such as when you pass them by reference as a parameter of another function. X is a local variable in main(), but it is passed by reference to foo(). Inside of foo(), the variable r can change the contents of x. When foo() is done, any changes that occured to r will be present in x, within main(). This is probably a better example:

Code:
void foo ( int v, int& r );

void main () {

int x, y;
x = 5;
y = 5;
foo(x, y);
cout << x;
cout << y;
};

void foo( int v, int& r ){

v = 10;
r = 10;
};

In this example, the output of the program would be:

5
10

Because x was passed by value, so v was created as a seperate variable with the same value x had when it was passed (5). V gets changed to 10, but since it's a different variable than x, x doesn't change. After foo() is run, x still has the value of 5. That's why the first line of the output (cout << x ) is 5. Y, on the other hand, is passed to foo() by reference, so r and y are the same chunk of memory. When r is given the value of 10, y is also changed to 10. So after foo() gets done, y is now equal to 10, and that's why the second line of the output (cout << y) is 10.

You can't use the variable names x and y within the function foo(), and you can't use the variable names v and r within the main() function, because they're all local variables, and only exist in the functions they are defined in. Pass-by-reference is kind of a way to get around that limitation. When you pass a local variable by reference, you are allowing its contents to be changed within a different function. The alternative is to make the variable a global variable instead of a local variable, so it can be used in any function at any time. This usually isn't a desirable thing to do for many reasons, so people often just use local variables and pass by reference when they need to access a variable in another function.
 
If you take Assembly @ any point, it will become more obvious what's really going on...

IMHO, that's the real benefit of taking Assembly courses.

I just bought an Advanced C# book recently, and most of the book covers MSIL (Microsoft Intermediate Language), which is the language that all .NET languages are compiled into. It's the same concept...But interesting to see what/how the compiler ends up treating things like function parameters (pass by ref, pass by val, etc).
 
Typedef Enum said:
If you take Assembly @ any point, it will become more obvious what's really going on...

IMHO, that's the real benefit of taking Assembly courses.

I just bought an Advanced C# book recently, and most of the book covers MSIL (Microsoft Intermediate Language), which is the language that all .NET languages are compiled into. It's the same concept...But interesting to see what/how the compiler ends up treating things like function parameters (pass by ref, pass by val, etc).

i agree

people are telling low level is useless, but when you understant low level, high level stuff became a lot easier.
 
Code:
people are telling low level is useless, but when you understant low level, high level stuff became a lot easier.

These are the same idiots who say code efficiency isn't an issue, because there is so much CPU power. Yes, you don't need to have SUPER optimised code for everything, but making sure you're using the right data structures and algorithms is VERY important, regardless.

In any case, you need to know the low-level since that's what was used to build up to the high level. It's all abstraction layers and if you don't understand them you won't be able to efficiently work within any one of them.
 
Back
Top