Make a variable available everywhere without passing?

This is a discussion on Make a variable available everywhere without passing? within the C++ Programming forums, part of the General Programming Boards category; Can I define a variable in main() in such a way that I can use it within functions called by ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    4

    Make a variable available everywhere without passing?

    Can I define a variable in main() in such a way that I can use it within functions called by main without having to pass it to the functions? Thanks in advance.

    Austin

  2. #2
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    No, you can't (to the very best of my knowledge). You could make it a global though.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    You don't necessarily have to make the variable itself global, but you can use a global pointer to change the value of a local variable that is declared in main from another function:

    Code:
    #include <iostream>
    
    int *foo;
    
    void func()
    {
        *foo = 10;
    }
    
    int main()
    {
        int bar = 0;
        foo = &bar;
    
        func();
    
        std::cout << bar << std::endl;  // Should output 10
    
    }
    Don't know why you'd really want to do that though.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    [quote]
    Don't know why you'd really want to do that though.
    [/quore]

    Yeah, was just wondering that :P
    Just makes it more work for the same purpose as just declaring a global, IMO.

  5. #5
    Reza Djahanbakhshi C++Lover's Avatar
    Join Date
    Mar 2005
    Location
    Iran
    Posts
    2
    I think he just want it's experience.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    main() is a function just like other functions in your program. main() is set off with braces just like other functions, which defines its block. The scope rules of functions apply to main() as well. Variables declared inside a block are destroyed when the block ends. Variables created with the new operator are an exception, and they continue to exist until they are deleted. However, other functions are not aware of the existence of any variables declared in other functions now matter how they were created. The only way another function can know of the existence of a variable declared in a function, is if the variable is passed to the second function.
    Last edited by 7stud; 03-02-2005 at 07:05 PM.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    4

    Thank you

    Thanks everyone for taking the time to post. The full story is this. I was given a somewhat complicated program that solves nonlinear sets of equations, and I am modifying it so that it can handle a particular set of equations to solve, and this requires a change in the structure of the original program. The program contains a lot of function calls, and functions that call functions. It would make it much easier for me if I could declare a few constants that I don't have to pass around, so that I don't have to mess with these predefined functions that link to header files. On top of this, I'm new to c++ so I'm not well versed in the language yet.

    From the helpful responses here, it seems I'll have to find another way to do what I need to. Thanks very much.

    Austin

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Functions are supposed to be like black boxes: you feed them some input, and they spit out some output. When you start using global variables, your code is no longer reusable because it depends on extraneous inputs, so it's generally considered poor style.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    4
    Quote Originally Posted by 7stud
    Functions are supposed to be like black boxes: you feed them some input, and they spit out some output. When you start using global variables, your code is no longer reusable because it depends on extraneous inputs, so it's generally considered poor style.
    The existing program has a function takes in a vector of variables and passes them through a corresponding vector of functions. There's an iterative procedure that eventually zeroes out the equations and thus solves for the vector of variables.

    What I'm trying to do is change it so that the user can provide the number of equations. So function needs to be able accept another input variable. And that change will necessarily ripple through other functions and a header file, as far as I can now tell. That's in addition to what else I have to modify with the functions themselves to get them to compute what I'm after, which is two-phase equilibrium.

    Austin

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    4

    I think I found a way to do it

    In main() I can write the variable value into a txt file, and inside each function that needs that value, I can have a statement that reads it from the txt file.

    This way, I can define my variable and its value in main() and have it accessible in all functions without having to disrupt the current structure of the functions and header files in order to be able to pass this new variable around.

    Thoughts or suggestions on this approach are welcome.

    Thanks,
    Austin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM
  4. Variable passing
    By seizmic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2005, 08:22 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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