Variable Scope and Lifetime

Cards (48)

  • A local variable has its scope within a function or block, while a global variable has its scope throughout an entire file.
  • Scope of a variable
    • Extent to which different parts of the program have access to the variable
    • Where the variable is visible
  • Lifetime of a variable
    • How long the variable persists in memory
    • Period from the point when the variable's storage is allocated to the point when it is deallocated
  • External variable
    • Variable defined outside of any function
    • Scope: entire source code file that contains this variable's definition
    • Lifetime: from its definition until program termination
  • Local variable
    • Variable defined within a function
    • Scope: limited to the function in which they are defined
  • The main() function has no external variables
  • The print_value() function has no external variables
  • Automatic variable

    • Local variables are automatic by default
    • Created anew each time the function is called
    • Destroyed when execution leaves the function
  • The output of the program with automatic variables is "First display. X: 1 Second display. X: 1"
  • Static variable
    Local variable that retains its value between calls to the function
  • The output of the program with static variables is "First display. X: 1 Second display. X: 2"
  • Learning Objectives
    • Determine the scope and lifetime of types of variables – external and local, static and automatic
    • Properly demonstrate passing argument/s to functions using pass-by-value method and pass-by-reference method
    • Demonstrate how to pass arrays to functions
  • Variable Scope
    The extent to which different parts of the program have access to the variable. It refers to where the variable is visible.
  • Variable Lifetime
    How long the variable persists in memory. It is a period from the point when the variable's storage is allocated to the point when it is deallocated.
  • Sample Program 1
    1. Declare variable x outside of any function
    2. Access x in main() function and print_value() function
    3. x persists until program termination
  • Sample Program 2
    1. Declare variable x inside main() function
    2. Cannot access x in print_value() function
    3. x still exists but cannot be accessed
  • Sample Program 3
    1. Declare variable x at global scope (Line 3)
    2. Declare variable x at local scope (Line 15)
    3. Local variable x takes priority when accessed in print_value() function
    4. Global variable x has longer lifetime than local variable x
  • Sample Program 4

    1. Declare variable x in main() function
    2. Declare variable x in print_value() function
    3. Scopes of the two x variables do not overlap
    4. Lifetime of x in main() is longer than x in print_value()
  • External Variable
    Variable declared outside of any function. Its scope is the entire source code file that contains this variable's definition. Its lifetime is from its definition until program termination.
  • Local Variable
    Variable defined within a function. The scope of local variables are limited to the function in which they are defined.
  • Automatic Variable

    Local variables that are created anew each time the function is called, and they are destroyed when execution leaves the function.
  • Static Variable
    Local variable that retains its value between calls to the function in which it is defined.
  • Sample Program 6
    1. Declare automatic variable x in display() function
    2. x is destroyed after each call to display()
    3. A new x is declared on each call
  • Sample Program 7
    1. Declare static variable x in display() function
    2. x retains its value between calls to display()
    3. Output shows x incrementing on each call
  • Functions, Arguments and Parameters
  • We already know what parameters and arguments are and how they work
  • Static variable
    A variable that retains its value between function calls
  • Sample Program 7
    1. Declare static int x
    2. Print "First display."
    3. Call display()
    4. Print "Second display."
    5. Call display()
  • Sample Program 7 output
    • First display.
    • X: 1
    • Second display.
    • X: 2
  • Formal parameter
    Parameter found in the function header, placeholder for data passed to the function
  • Actual parameter (argument)
    Parameter specified in the function call, the data that needs to be passed to the function
  • Pass by value
    • The function is passed a copy of the variable's value, the function has no access to the original variable and cannot modify it
  • Pass by reference
    • The function is passed a reference to (address of) the original variable, the function has access to the original variable and can modify it
  • Passing by reference
    Allows the function to modify the argument variable
  • Passing by value
    Does not allow the function to modify the argument variable
  • Sample Program 9
    1. Declare x=2, y=4
    2. Call by_value(x, y)
    3. Call by_reference(&x, &y)
  • Sample Program 9 output
    • Before by_value(), x = 2, y = 4.
    • After by_value(), x = 2, y = 4.
    • Before by_reference(), x = 2, y = 4.
    • After by_reference(), x = 0, y = 0.
  • Passing 1D arrays to functions
    • Can use pointer notation, array notation without size, or array notation with size
  • Passing 2D arrays to functions
    • Can specify all sizes (subscript values) or only the leftmost size (subscript value)
  • The name of an array is a pointer to the array's first element