|
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:
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:
|