Variable Q:

This is a discussion on Variable Q: within the C++ Programming forums, part of the General Programming Boards category; I want to make a function that can declare variables or access variables and recurse or loop some functions without ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    60

    Variable Q:

    I want to make a function that can declare variables or access variables and recurse or loop some functions without re-declared. For example:
    Code:
    int hi()
    {
    int small = 1;
    int health = 15;
    //In the process health will change so recursing re-declaring it will overwrite the existing variable and I need health to stay as is.
    hi()     //recursing hi wil declare the variable health at 15 and in the process it will change to something else.
    EDIT: I know! will goto work? to goto the process?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If I understand your problem correctly, you could try the static keyword. Here's a short example:
    Code:
    #include <iostream>
    
    void func()
    {
    static int var = 1;
    if(var==1)
    {
      std::cout<<var++;
      func();
    }
    }
    
    int main()
    {
    func();
    }
    In that example var is initialized with a value of 1, then it outputs var and increments it. Then the function is called again and var has a value of 2, so there is no output and the function returns. It shouldn't be too hard to find a better explanation of the static keyword by searching these boards or google.

    Another option could be to simply have a global variable, although static might be the prefered method.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    There's a few options you have. The two most obvious ones JaWib has already mentioned (use a global variable or declare the variable as static). But you could also declare the variable in main(), and pass it as a reference to the function. That way the variable can be used by main() if need be, and its value doesn't get reset each time the function runs, and you still haven't created one of those hated-by-many-people global variables

    EDIT: I know! will goto work? to goto the process?
    You should be careful when you say stuff like that around here, some people might take offense (do a search for 'goto evil' on this messageboard)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    That's what I wanted to do, make a global variable or make the variable a parameter. static keywords are evil! But how do you make a global variable?
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    static keywords are evil!
    Errrr... why?

    How do you make a global variable?
    Very simple. Declare it outside of any function (at the top of your main .cpp file).
    Code:
    int small; //or int small = 1;
    int health; // or int health = 15;
     
    int hi();
     
    int main()
    {
       //if you didn't initialize small and health at the top:
       small = 1;
       health = 15;
       hi();
     
       return 0;
    }
     
    int hi()
    {
       //do something with small and health, recurse, whatever you want :)
    }
    Last edited by Hunter2; 07-07-2004 at 03:45 PM. Reason: Forgot "return 0;" :O
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>static keywords are evil!

    I beg to differ. It might not be the best to use a static variable in this case, but if you can use it (ie you only use the variable in one function), it is better than a global variable IMO. Plus, it can be very useful in classes.

    But it's your program anyways so do what you want
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Plus, it can be very useful in classes.
    Oh yeah, while we're on the subject... where do you usually put your static class member declarations (or definitions, whatever you call it)? Do you just stick them at the top of your main.cpp or something? Because I'm starting to get annoyed with my huge long statics.h header that I include in main.cpp...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    I usually stick it in the myclassname.cpp file before I define/declare all the member functions.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oops. Should have thought of that
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

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