Local Variables

Definition

  • Variables declared inside a function.
  • Scope is limited to the function where declared.
  • Initialization

  • Can be initialized by any expression.
  • Initialized on every function call.
  • Scope and Lifetime

  • **Function Scope:** Limited to the function body.
  • **Block Scope:** Variables declared within internal blocks ({}) have block scope, ending at the block's closing brace.
  • **Nested Blocks:** Identifiers in internal blocks hide external ones with the same name.
  • **Function Parameters:** Treated as local variables with function scope.
  • **static Local Variables:** Have block scope but persist throughout program execution, stored separately from the stack.
  • Memory Management

  • **Stack:** Allocated for non-static local variables. Default size: 256 KB. Managed by #property stacksize.
  • **Stack Overflow:** Occurs when nested function calls exhaust stack memory.
  • **Dynamic Memory:** Recommended for large local data. Allocate using new or ArrayResize() and release using delete or ArrayFree().
  • **Static/Global Variables:** Stored in a separate memory area.
  • **Dynamically Created Variables:** Use memory separate from the stack.
  • Examples

  • Function with local variable initialization.
  • Nested block scope demonstrating identifier hiding.
  • for loop scope demonstrating identifier hiding.
  • Related Topics

  • [Data Types](/basis/types)
  • [Encapsulation and Extensibility of Types](/basis/oop/incapsulation)
  • [Initialization of Variables](/basis/variables/initialization)
  • [Visibility Scope and Lifetime of Variables](/basis/variables/variable_scope)
  • [Creating and Deleting Objects](/basis/variables/object_live)