TDLByte #25 : Static Variables

Discussion in 'Tally & TDL Learning HUB' started by admin, Apr 1, 2016.

    
  1. admin

    admin Administrator Staff Member


    [​IMG]

    [​IMG]

    Static Variables

    TDLByte #25

    [​IMG]

    Dear Developers,

    The previous TDLBytes gave an overview of Scopes of Variables. With this background, we will start a new topic and explain a type of variable called 'Static Variables'.

    What is it?

    A variable can be declared static only inside a function. These variables are initialized only once and they persist their values between function calls.

    Let's understand the meaning of the same with an example in comparison to normal variable.

    [​IMG]

    [​IMG]

    [​IMG]

    First while loop
    Counter = 1
    NCounter
    Initial: 1
    Log : 2
    Second while loop
    Counter = 2
    NCounter
    Initial: 1
    Log : 2
    Third while loop
    Counter = 3
    NCounter
    Initial: 1
    Log : 2

    First while loop
    Counter = 1
    SCounter
    Initial: 1
    Log : 2
    Second while loop
    Counter = 2
    SCounter
    Initial: 2
    Log : 3
    Third while loop
    Counter = 3
    SCounter
    Initial: 3
    Log : 4


    Example explained:

    • The functions 'Nrmvar' and 'StatVar' have 3 successive calls in the function 'VarVals'.

    • Variable 'Ncounter' initialized for every call done to 'NrmVar'.

    • Static variable 'SCounter' initialized only once during the first call done to the function
    'StatVar'. Thus initial value for every successive call is the value of the variable at the end of the previous call.

    Stay tuned!
     


Share This Page