Top
Back: 3.7.1 Procedure definition
Forward: 3.7.3 Parameter list
FastBack: 3. General concepts
FastForward: 4. Data types
Up: 3.7 Procedures
Top: Singular 2-0-4 Manual
Contents: Table of Contents
Index: F. Index
About: About This Document

3.7.2 Names in procedures

All variables are local to the procedure they are defined in. Locally defined variables cannot interfere with names in other procedures and are automatically deleted after leaving the procedure.

Internally, local variables are stored using the nesting level. A variable is said to have nesting level 1, if it is local to a procedure that was called interactively, nesting level 2, if it is local to a procedure that was called by a procedure of nesting level 1 etc. listvar() also displays the nesting level, nesting level 0 is used for global objects (see listvar). A ring may be ’moved up’ by one nesting level with keepring (see keepring). All variables living in that ring are moved together with that ring.

To keep local variables after leaving the procedure, they have to be exported (i.e. made known) to some higher level by the export command (see export).

Example:

 
proc xxx
{
  int k=4;        //defines a local variable k
  int result=k+2;
  export(result);  //defines the global variable "result".
}
xxx();
listvar(all);
→ // result               [0]  int 6
→ // LIB                  [0]  string standard.lib

Note that the variable result became a global variable after the execution of xxx.


Top Back: 3.7.1 Procedure definition Forward: 3.7.3 Parameter list FastBack: 3. General concepts FastForward: 4. Data types Up: 3.7 Procedures Top: Singular 2-0-4 Manual Contents: Table of Contents Index: F. Index About: About This Document
            User manual for Singular version 2-0-4, October 2002, generated by texi2html.