A method is a block of organized, reusable code that is used to perform a single, related action. Methods provide better modularity for your application and a high degree of code reusing.
As you already know, Spla gives you many built-in methods like int<>, etc. but you can also create your own methods. These methods are called user-defined methods.
You can define methods to provide the required methodality. Here are simple rules to define a method in Spla.
Method blocks begin with the keyword # followed by the method name and parentheses (< >).
Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
The first statement of a method can be an optional statement - the documentation string of the method or docstring.
The code block within every method starts with a colon (:) and is indented.
The statement <- [expression] exits a method, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.
#methodname< parameters > returnType: ~~method_docstring~~ method_suite; <- [expression];
By default, parameters have a positional behavior and you need to inform them in the same order that they were defined.
The following method takes a string as input parameter and prints it on standard screen.
#printme< str strvar > int: ~~This prints a passed string into this method~~ << strvar; <- 0;
Defining a method gives it a name, specifies the parameters that are to be included in the method and structures the blocks of code.
Once the basic structure of a method is finalized, you can execute it by calling it from another method or directly from the Spla prompt. Following is an example to call the printme() method −
!! Method definition is here #printme<str strvar > int: ~~This prints a passed string into this method~~ << strvar; <- 0; #start<str arg[]>: !! Now you can call printme method printme<'This is first call to the user defined method!'>; printme<'Again second call to the same method'>;
When the above code is executed, it produces the following result −
This is first call to the user defined method! Again second call to the same method
All variables in a program may not be accessible at all locations in that program. This depends on where you have declared a variable.
The scope of a variable determines the portion of the program where you can access a particular identifier. There are two basic scopes of variables in Spla −
Variables that are defined inside a method body have a local scope, and those defined outside have a global scope.
This means that local variables can be accessed only inside the method in which they are declared, whereas global variables can be accessed throughout the program body by all methods. When you call a method, the variables declared inside it are brought into scope. Following is a simple example −
int total = 0 !! This is global variable. !! Method definition is here #sum<int arg1,int arg2 >: !! Add both the parameters and return them." total = arg1 + arg2; !! Here total is local variable. << 'Inside the method local total : '+ total; <- total; #start<str arg[]>: !! Now call sum method sum< 10, 20 >; << 'Outside the method global total : '+ total ;
When the above code is executed, it produces the following result −
Inside the method local total : 30 Outside the method global total : 0