Static definition of variables

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 ...

  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
    21,456
    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

  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
    21,456
    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.
    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

  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
    21,456
    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.
    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

  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
    21,456
    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.
    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

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    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?
    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

  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
    21,456
    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?
    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

  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, 04:26 PM
  4. static variables and includes?
    By Devils Child in forum C++ Programming
    Replies: 68
    Last Post: 01-30-2008, 09:59 AM
  5. Replies: 5
    Last Post: 11-19-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21