C++ Subroutines

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • compman9902
    New Member
    • Mar 2007
    • 105

    C++ Subroutines

    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.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by compman9902
    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.
    No 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

    Comment

    • compman9902
      New Member
      • Mar 2007
      • 105

      #3
      Originally posted by JosAH
      No 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
      bad practice, eh?
      well, I REALLY need this, so...how do you do global variables?
      thanks

      Comment

      • svlsr2000
        Recognized Expert New Member
        • Feb 2007
        • 181

        #4
        Though its a bad practice You can use Macros. but u have to make sure you dont use the macro any where else.

        Comment

        • Prasannaa
          New Member
          • May 2007
          • 21

          #5
          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

          • compman9902
            New Member
            • Mar 2007
            • 105

            #6
            Originally posted by Prasannaa
            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.
            that sounds like a great solution
            it would be fantasticif someone--anyone could post some code on how to do that
            thanks

            Comment

            Working...