Variables are nothing but reserved memory locations to store values. This means when you create a variable, you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables. Euphoria data types are explained in different chapter.
These memory locations are called variables because their value can be changed during their life time.
Variable Declaration
Euphoria variables have to be explicitly declared to reserve memory space. Thus declaration of a variable is mandatory before you assign a value to a variable.
Variable declarations have a type name followed by a list of the variables being declared. For example ā
integer x, y, z sequence a, b, x
When you declare a variable, you name the variable and you define which sort of values may legally be assigned to the variable during execution of your program.
The simple act of declaring a variable does not assign any value to it. If you attempt to read it before assigning any value to it, Euphoria will issue a run-time error as “variable xyz has never been assigned a value”.
Assigning Values
The equal sign (=) is used to assign values to variables. Variable can be assigned in the following manner ā
Variable_Name = Variable_Value
For example ā
#!/home/euphoria/bin/eui -- Here is the declaration of the variables. integer counter integer miles sequence name counter = 100 -- An integer assignment miles = 1000.0 -- A floating point name = "John" -- A string ( sequence ) printf(1, "Value of counter %d\n", counter ) printf(1, "Value of miles %f\n", miles ) printf(1, "Value of name %s\n", {name} )
Here 100, 1000.0 and “John” are the values assigned to counter, miles and name variables, respectively. This program produces the following result ā
Value of counter 100 Value of miles 1000.000000 Value of name John
To guard against forgetting to initialize a variable, and also because it may make the code clearer to read, you can combine declaration and assignment ā
integer n = 5
This is equivalent to the following ā
integer n n = 5
Identifier Scope
The scope of an identifier is a description of what code can access it. Code in the same scope of an identifier can access that identifier and code not in the same scope as identifier cannot access it.
The scope of a variable depends upon where and how it is declared.
- If it is declared within aĀ for, while, loopĀ orĀ switch, its scope starts at the declaration and ends at the respectiveĀ endĀ statement.
- In anĀ ifĀ statement, the scope starts at the declaration and ends either at the nextĀ else, elsifĀ orĀ end ifĀ statement.
- If a variable is declared within a routine, the scope of the variable starts at the declaration and ends at the routine’s end statement. This is knows as a private variable.
- If a variable is declared outside of a routine, its scope starts at the declaration and ends and the end of the file it is declared in. This is known as a module variable.
- The scope of aĀ constantĀ that does not have a scope modifier, starts at the declaration and ends and the end of the file it is declared in.
- The scope of aĀ enumĀ that does not have a scope modifier, starts at the declaration and ends and the end of the file it is declared in.
- And The scope of allĀ procedures, functionsĀ andĀ types, which do not have a scope modifier, starts at the beginning of the source file and ends at the end of the source file in which they are declared.
Locals
Constants, enums, module variables, procedures, functions, and types, which do not have a scope modifier are referred to asĀ locals. However, these identifiers can have a scope modifier preceding their declaration, which causes their scope to extend beyond the file they are declared in.
- If the keyword global precedes the declaration, the scope of these identifiers extends to the whole application. They can be accessed by code anywhere in the application files.
- If the keyword public precedes the declaration, the scope extends to any file that explicitly includes the file in which the identifier is declared, or to any file that includes a file that in turn public includes the file containing the public declaration.
- If the keyword export precedes the declaration, the scope only extends to any file that directly includes the file in which the identifier is declared.
When youĀ includeĀ a Euphoria file in another file, only the identifiers declared using a scope modifier are accessible to the file doing theĀ include. The other declarations in the included file are invisible to the file doing theĀ include.
Next Topic:-Click Here