I have this:
But in sumb function i get the error that b is not declared (first time in use). How can i solve this?Code:int main() { int b; b=1 sumb(); } void sumb() { b+=3;}
Thanks!
I have this:
But in sumb function i get the error that b is not declared (first time in use). How can i solve this?Code:int main() { int b; b=1 sumb(); } void sumb() { b+=3;}
Thanks!
Pass the address of b to sumb as an argument, then change b += 3 to *b += 3.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You could pass b by value, add 3, then return the result.Isn't there another way?
You could also make b a global variable, but I would recommend that you do not do that.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
They make it more difficult to reason about your program and maintain it. For example, if b was a global:Why not?
- b could be changed from anywhere, especially where the change was least expected.
- It is less obvious that b is an input variable of sumb() than if it were passed as an argument.
- Functions that manipulate global variables can be less reusable, e.g., sumb() could only ever to used to sum b (which is probably what you were thinking when you named it sumb).
- Renaming b means having to rename it everywhere.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Why?In this case that's exactly what i want, a unique function just for controlling b and nothing more.
Yes, and not in any other function or struct.How and where should i define the globals? Outside main()?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Because it is a specific function to change some specific variables in a unique way.
b in this case works like an increment to a pointer i'm using, i wont have another pointers so i like to write sumb() when calling the function instead of sumb(b).
And the name tells me what var is changed sumb.
Thanks!
keyword here is ME... It tells noone else...And the name tells me
When someone sees the code
He has no idea what is goiing on inside the functionCode:sumb();
When someone sees the code
He can suppose that b is modified insideCode:sumb(&b);
when someone sees the code
He knows for sure that b is modified hereCode:b=Sumb(b);
Of course - giving b a talking name, and naming sumb in some reasonable way will also help...
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
You may be better off not writing a function at all, since this all so unique.Because it is a specific function to change some specific variables in a unique way.
Using a global variable to avoid another level of indirection is probably a Bad Thing. There may be other, better, solutions. What exactly are you really trying to do?b in this case works like an increment to a pointer i'm using, i wont have another pointers so i like to write sumb() when calling the function instead of sumb(b).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Why learn bad habits?You're right but i'm just learning, this code i'm doing will not be seen by anyone else :P
Its just a way for me to know all possible ways of doing a function, even the useless ways like this one.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)