top of page
  • Writer's pictureAdisorn O.

GLOBAL VS. LOCAL VARIABLES

Updated: Mar 19, 2023


Local variables exist only within their scope—the main program or the user-defined functions. In contrast, global variables exist everywhere inside the program. This simple example shows how the value of the global variable is changed inside the function.


First, a is known as the local variable inside function change. Since it has given no value, the operation with this variable is an error.












Now, if we define "a" as a global variable, the value of "a" is automatically retrieved from the caller (main program). Any new assignment to "a" will forever change its value after returning to the main program.


* It is necessary to declare global variables in both main and functions if the functions are called from an external m-file. In this example, it is not mandatory since the function is written in the same m-mail as the main program.

















17 views

Comments


bottom of page