<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <HTML> <HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE> A Guide to the S-Lang Language: Variables</TITLE> <LINK HREF="slang-7.html" REL=next> <LINK HREF="slang-5.html" REL=previous> <LINK HREF="slang.html#toc6" REL=contents> </HEAD> <BODY> <A HREF="slang-7.html">Next</A> <A HREF="slang-5.html">Previous</A> <A HREF="slang.html#toc6">Contents</A> <HR> <H2><A NAME="s6">6. Variables</A></H2> <P> <P>A variable must be declared before it can be used, otherwise an undefined name error will be generated. A variable is declared using the <CODE>variable</CODE> keyword, e.g, <BLOCKQUOTE><CODE> <PRE> variable x, y, z; </PRE> </CODE></BLOCKQUOTE> declares three variables, <CODE>x</CODE>, <CODE>y</CODE>, and <CODE>z</CODE>. This is an example of a variable declaration statement, and like all statements, it must end in a semi-colon. <P>Variables declared this way are untyped and inherit a type upon assignment. The actual type checking is performed at run-time. For example, <BLOCKQUOTE><CODE> <PRE> x = "This is a string"; x = 1.2; x = 3; x = 2i; </PRE> </CODE></BLOCKQUOTE> results in x being set successively to a string, a float, an integer, and to a complex number (<CODE>0+2i</CODE>). Any attempt to use a variable before it has acquired a type will result in an uninitialized variable error. <P>It is legal to put executable code in a variable declaration list. That is, <BLOCKQUOTE><CODE> <PRE> variable x = 1, y = sin (x); </PRE> </CODE></BLOCKQUOTE> are legal variable declarations. This also provides a convenient way of initializing a variable. <P>Variables are classified as either <EM>global</EM> or <EM>local</EM>. A variable declared inside a function is said to be local and has no meaning outside the function. A variable is said to be global if it was declared outside a function. Global variables are further classified as being <CODE>public</CODE>, <CODE>static</CODE>, or <CODE>private</CODE>, according to the name space where they were defined. See chapter ??? for more information about name spaces. <P>The following global variables are predefined by the language and are mainly used as convenience variables: <BLOCKQUOTE><CODE> <PRE> $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 </PRE> </CODE></BLOCKQUOTE> <P>An <EM>intrinsic</EM> variable is another type of global variable. Such variables have a definite type which cannot be altered. Variables of this type may also be defined to be read-only, or constant variables. An example of an intrinsic variable is <CODE>PI</CODE> which is a read-only double precision variable with a value of approximately <CODE>3.14159265358979323846</CODE>. <P> <P> <HR> <A HREF="slang-7.html">Next</A> <A HREF="slang-5.html">Previous</A> <A HREF="slang.html#toc6">Contents</A> </BODY> </HTML>