Hello, and thank you for viewing this thread. I was just wondering if there was a way to have subroutines, like in BASIC, in C++ that can use all of the same variables that main() does.
C++ Subroutines
Collapse
X
-
Tags: None
-
Originally posted by compman9902Hello, and thank you for viewing this thread. I was just wondering if there was a way to have subroutines, like in BASIC, in C++ that can use all of the same variables that main() does.
another function call. You can have global variables though which are visible
everywhere but are considered bad practice. An alternative would be to pass
references of the local variables to the called function.
Try to study a bit on object oriented programming and you'll see that you hardly
need global variables (Basic uses them a lot).
kind regards,
Jos -
Originally posted by JosAHNo you can't: local variables have lexical scoping, i.e. they aren't visible in
another function call. You can have global variables though which are visible
everywhere but are considered bad practice. An alternative would be to pass
references of the local variables to the called function.
Try to study a bit on object oriented programming and you'll see that you hardly
need global variables (Basic uses them a lot).
kind regards,
Jos
well, I REALLY need this, so...how do you do global variables?
thanksComment
-
Hi ,
Since i am not aware of the precise context, I assume that you need all the other functions to use vatiables declared in main(). You can do so my moving the variables to a struct , declare the struct in main, and change those functions to take this struct as argument(pass by ref). I think should solve the problem.
Regards,
Prasannaa.Comment
-
Originally posted by PrasannaaHi ,
Since i am not aware of the precise context, I assume that you need all the other functions to use vatiables declared in main(). You can do so my moving the variables to a struct , declare the struct in main, and change those functions to take this struct as argument(pass by ref). I think should solve the problem.
Regards,
Prasannaa.
it would be fantasticif someone--anyone could post some code on how to do that
thanksComment
Comment