Thread: Static definition of variables

  1. #1
    Banned
    Join Date
    Mar 2008
    Posts
    78

    Static definition of variables

    I have this:

    Code:
    int main() {
    
    int b;
    b=1
    
    sumb();
    }
    
    void sumb() {
    b+=3;}
    But in sumb function i get the error that b is not declared (first time in use). How can i solve this?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Pass the address of b to sumb as an argument, then change b += 3 to *b += 3.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by laserlight View Post
    Pass the address of b to sumb as an argument, then change b += 3 to *b += 3.
    Isn't there another way?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Isn't there another way?
    You could pass b by value, add 3, then return the result.

    You could also make b a global variable, but I would recommend that you do not do that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by laserlight View Post
    You could pass b by value, add 3, then return the result.

    You could also make b a global variable, but I would recommend that you do not do that.
    Why not?
    (im very new to C, so sorry for some very dumb question i may ask).

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why not?
    They make it more difficult to reason about your program and maintain it. For example, if b was a global:
    • 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by laserlight View Post
    They make it more difficult to reason about your program and maintain it. For example, if b was a global:
    • 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.
    In this case that's exactly what i want, a unique function just for controlling b and nothing more.

    How and where should i define the globals? Outside main()?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In this case that's exactly what i want, a unique function just for controlling b and nothing more.
    Why?

    How and where should i define the globals? Outside main()?
    Yes, and not in any other function or struct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by laserlight View Post
    Why?
    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!

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And the name tells me
    keyword here is ME... It tells noone else...

    When someone sees the code
    Code:
    sumb();
    He has no idea what is goiing on inside the function

    When someone sees the code
    Code:
    sumb(&b);
    He can suppose that b is modified inside

    when someone sees the code
    Code:
    b=Sumb(b);
    He knows for sure that b is modified here

    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

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Because it is a specific function to change some specific variables in a unique way.
    You may be better off not writing a function at all, since this all so unique.

    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).
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by vart View Post
    keyword here is ME... It tells noone else...

    When someone sees the code
    Code:
    sumb();
    He has no idea what is goiing on inside the function

    When someone sees the code
    Code:
    sumb(&b);
    He can suppose that b is modified inside

    when someone sees the code
    Code:
    b=Sumb(b);
    He knows for sure that b is modified here

    Of course - giving b a talking name, and naming sumb in some reasonable way will also help...
    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.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Why learn bad habits?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by laserlight View Post
    Why learn bad habits?
    I'm not learning bad habits, i'm learning bad ways to do things, and i'm learning the good ways too. But you never know when a bad way may be your only way to do something.
    Has i said before, i wont use this method often, so i'm not learning bad habits.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. defining macros or static variables
    By l2u in forum C++ Programming
    Replies: 15
    Last Post: 08-05-2008, 06:28 AM
  2. Calling non-static functions on static variables
    By pandu in forum C++ Programming
    Replies: 14
    Last Post: 06-19-2008, 03:07 AM
  3. const correctness and static member variables
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 02-21-2008, 05:26 PM
  4. static variables and includes?
    By Devils Child in forum C++ Programming
    Replies: 68
    Last Post: 01-30-2008, 10:59 AM
  5. Replies: 5
    Last Post: 11-19-2002, 07:49 PM