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
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
No, you can't (to the very best of my knowledge). You could make it a global though.
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:
Don't know why you'd really want to do that though.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 }
"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
[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.
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 08:05 PM.
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
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.Originally Posted by 7stud
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
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