Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Global variables

    Help
    3
    8
    386
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      petermeisenstein F last edited by

      How to set a variable global

      1 Reply Last reply Reply Quote 0
      • Hector1945
        Hector1945 F last edited by

        all variables are global by default, except the ones in functions

        P 1 Reply Last reply Reply Quote 0
        • P
          petermeisenstein F @Hector1945 last edited by

          @Hector1945 And if I want to use them in functions?

          1 Reply Last reply Reply Quote 0
          • PB____
            PB____ last edited by

            If you have declared the variable outside of the function, before using it in the function, it is global.

            For example:

            int myVariable = 0; // This variable is now declared in the global scope (and I assigned the value 0)
            function setMyVariable(value)
                myVariable = value // assign a new value to myVariable
            return value
            print(myVariable, "\n") // prints 0
            int returnValue = setMyVariable(1)
            print(returnValue, "\n") // prints 1
            print(myVariable, "\n") // prints 1
            

            if you want to make sure the variable is scoped to the function only, you need to declare it in the function:

            int myVariable = 0; // This variable is now declared in the global scope (and I assigned the value 0)
            function setMyVariable(value)
                int myVariable = value // by beginning with the type, I'm declaring a new variable at the scope of the function (and I assign value to it)
            return myVariable
            print(myVariable, "\n") // prints 0
            int returnValue = setMyVariable(1)
            print(returnValue, "\n") // prints 1
            print(myVariable, "\n") // still prints 0
            
            P 1 Reply Last reply Reply Quote 2
            • P
              petermeisenstein F @PB____ last edited by

              @PB____ Thanks

              1 Reply Last reply Reply Quote 0
              • P
                petermeisenstein F last edited by

                Is this pseudo code ?

                1 Reply Last reply Reply Quote 0
                • PB____
                  PB____ last edited by

                  No, not really, but I did accidentally used a ; on the first line. If you remove that, it should work.

                  P 1 Reply Last reply Reply Quote 0
                  • P
                    petermeisenstein F @PB____ last edited by

                    @PB____ C++ left the chat

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post