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!
This is a discussion on Static definition of variables within the C Programming forums, part of the General Programming Boards category; I have this: Code: int main() { int b; b=1 sumb(); } void sumb() { b+=3;} But in sumb function ...
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.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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()?
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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...
If I have eight hours for cutting wood, I spend six sharpening my axe.
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).
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way