|
3.7.1 Procedure definition
- Syntax:
- [
static ] proc proc_name [(<parameter_list>)]
[<help_string>]
{
<procedure_body>
}
[example
{
<sequence_of_commands>
} ]
- Purpose:
-
Defines a new function, the
proc proc_name.
-
The help string, the parameter list, and the example section are optional.
They are, however, mandatory for the procedures listed in the header of a library.
The help string is ignored and no example section is allowed if the procedure is defined
interactively, i.e., if it is not loaded from a file by the
LIB or load
command (see LIB and see load ).
-
Once loaded from a file into a SINGULAR session, the information provided in the help
string will be displayed upon entering
help proc_name; , while the example
section will be executed upon entering example proc_name; .
See Parameter list, Help string, and the example in
Procedures in a library.
-
In the body of a library, each procedure not meant to be accessible by
users should be declared static. See Procedures in a library.
Example of an interactive procedure definition and its execution:
| proc milnor_number (poly p)
{
ideal i= std(jacob(p));
int m_nr=vdim(i);
if (m_nr<0)
{
"// not an isolated singularity";
}
return(m_nr); // the value of m_nr is returned
}
ring r1=0,(x,y,z),ds;
poly p=x^2+y^2+z^5;
milnor_number(p);
==> 4
|
Example of a procedure definition in a library:
First, we define the library (and store it as sample.lib ):
| // Example of a user accessible procedure
proc tab (int n)
"USAGE: tab(n); n integer
RETURNS: string of n space tabs
EXAMPLE: example tab; shows an example"
{ return(internal_tab(n)); }
example
{
"EXAMPLE:"; echo=2;
for(int n=0; n<=4; n=n+1)
{ tab(4-n)+"*"+tab(n)+"+"+tab(n)+"*"; }
}
// Example of a static procedure
static proc internal_tab (int n)
{ return(" "[1,n]); }
|
Now, we load the library and execute its procedures:
| LIB "sample.lib"; // load the library sample.lib
example tab; // show an example
==> // proc tab from lib sample.lib
==> EXAMPLE:
==> for(int n=0; n<=4; n=n+1)
==> { tab(4-n)+"*"+tab(n)+"+"+tab(n)+"*"; }
==> *+*
==> * + *
==> * + *
==> * + *
==> * + *
==>
"*"+tab(3)+"*"; // use the procedure tab
==> * *
// the static procedure internal_tab is not accessible
"*"+internal_tab(3)+"*";
==> ? `internal_tab(3)` is not defined
==> ? error occurred in or before ./examples/Example_of_a_procedure_defini\
tion_in_a_library:.sing line 5: ` "*"+internal_tab(3)+"*";`
// show the help section for tab
help tab;
==> // ** Could not get 'IdxFile'.
==> // ** Either set environment variable 'SINGULAR_IDX_FILE' to 'IdxFile',
==> // ** or make sure that 'IdxFile' is at "%D/singular/singular.idx"
==> // ** Displaying help in browser 'dummy'.
==> // ** Use 'system("--browser", <browser>);' to change browser,
==> // ** where <browser> can be: "dummy", "emacs".
==> ? No functioning help browser available.
==> ? error occurred in or before ./examples/Example_of_a_procedure_defini\
tion_in_a_library:.sing line 7: ` help tab;`
|
|