An overview
A subroutine is like a sub-program. It is not only helps divide your code up into sensible, manageable chunks, but it also allows these chunks to be used (called) by different parts of your program. Each subroutine contains one of more statements.
In common with other languages, SmartMS provides 2 types of subroutine
Procedures and Functions.
Functions are the same as procedures except that they return a value in addition to executing statements. A Function, as its name suggests, is like a little program that calculates something, returning the value to the caller. On the other hand, a procedure is like a little routine that performs something, and then just finishes.
Parameters to subroutines
Both functions and procedures can be defined to operate without any data being passed. For example, you might have a function that simply returns a random number (like the SmartMS RandomInt function). It needs no data to get it going.
Likewise, you can have a procedure that carries out some task without the need for data to dictate its operations. For example, you might have a procedure that draws a square on the screen. The same square every time it is called.
Often, however, you will pass data, called parameters, to a subroutine. (Note that the definition of a subroutine refers to parameters as arguments - they are parameters when passed to the subroutine).
Some simple function and procedure examples
The following code illustrates simple function and procedure definitions:
Code example: procedure without parameters
procedure ShowTime; // A procedure with no parameters
begin
// Display the current date and time
WriteLn('Date and time is '+DateTimeToStr(Now));
end;
// Let us call this procedure
procedure TForm1.btnClearClick(Sender: TObject);
begin
ShowTime;
end;
|
Result is: Date and time is 2014-07-17 13:55:01
function ShowTime() {
WriteLn(("Date and time is "+DateTimeToStr(Now())));
};
function btnClearClick(Self, Sender$4) {
ShowTime();
}
|
Notice that we are using some SmartMS run time library functions.
A procedure with parameters in SmartMS
Example code : Show yesterday, today and tomorrows dates in SmartMS
procedure ShowTime(dateTime : Float); // With parameters
begin
// Display the date passed to the routine
WriteLn('Date and time is '+DateToStr(dateTime));
end;
function Today: Float;
begin
Result := Date;
end;
function Yesterday: Float;
begin
Result := Date - 1;
end;
function Tomorrow: Float;
begin
Result := Date + 1;
end;
// Let us call this procedure
procedure TForm1.btnClearClick(Sender: TObject);
begin
ShowTime(Today);
ShowTime(Yesterday);
ShowTime(Tomorrow);
end;
|
Result is:
Date and time is 2014-07-17
Date and time is 2014-07-16
Date and time is 2014-07-18
--------------------------------------------
Tip: TDateTime in Delphi is equal to Float in SmartMS.
TDateTime = Float;
Real = Float;
Double = Float;
Extended = Float;
TColor = Integer;
THandle = Variant;
|
A function without parameters in SmartMS
Example code : Return a random character A-Z in SmartMS
function RandomRange(const AFrom, ATo: Integer): Integer;
begin
if AFrom > ATo then
Result := RandomInt(AFrom - ATo) + ATo
else
Result := RandomInt(ATo - AFrom) + AFrom;
end;
function RandomChar : String;
var
i : integer;
begin
// Get a random number from 65 to 90
// (These numbers equate to characters 'A' to 'Z'
i := RandomRange(65, 90);
// Return this value as a char type in the return variable, Result
Result := Chr(i);
end;
procedure TForm1.btnClearClick(Sender: TObject);
begin
// Let us call this function
ShowMessage('Char chosen is : '+RandomChar);
end;
|
Result is: Char chosen is : W
function RandomChar() {
var Result = "";
var i = 0;
i = RandomRange(65,90);
Result = Chr(i);
return Result
};
function RandomRange(AFrom, ATo) {
var Result = 0;
if (AFrom>ATo) {
Result = RandomInt(AFrom-ATo)+ATo;
} else {
Result = RandomInt(ATo-AFrom)+AFrom;
}
return Result
};
|
It is important to note that we return the value from a function in a special variable called Result that SmartMS
secretly defines for us to be the same type as the return type of the function. We can assign to it at any point in the function.
When the function ends, the value then held in Result is then returned to the caller.
A function with parameters in SmartMS
Example code : Calculate the mean of a set of 3 numbers using Delphi
function Average(a, b, c : Extended) : Extended;
begin
// return the average of the 3 passed numbers
Result := Mean(a, b, c);
end;
// Let us call this function
ShowMessageFmt('Average of 2, 13 and 56 = %f',[Average(2,13,56)]);
|
NOTE: I would like to calculate the average of a set of three numbers. In Delphi you could use the mean function.
The Mean function returns the average of a set of Double values in a DataArray. As you can see, such function uses a
low level function SUM coded in Assembler language.
function Mean(const Data: array of Double): Extended;
begin
Result := SUM(Data) / (High(Data) - Low(Data) + 1);
end;
|
function SUM(const Data: array of Double): Extended;
asm // IN: EAX = ptr to Data, EDX = High(Data) = Count - 1
// Uses 4 accumulators to minimize read-after-write delays and loop overhead
// 5 clocks per loop, 4 items per loop = 1.2 clocks per item
FLDZ
MOV ECX, EDX
FLD ST(0)
AND EDX, not 3
FLD ST(0)
AND ECX, 3
FLD ST(0)
SHL EDX, 3 // count * sizeof(Double) = count * 8
JMP @Vector.Pointer[ECX*4]
@Vector:
DD @@1
DD @@2
DD @@3
DD @@4
@@4: FADD qword ptr [EAX+EDX+24] // 1
FXCH ST(3) // 0
@@3: FADD qword ptr [EAX+EDX+16] // 1
FXCH ST(2) // 0
@@2: FADD qword ptr [EAX+EDX+8] // 1
FXCH ST(1) // 0
@@1: FADD qword ptr [EAX+EDX] // 1
FXCH ST(2) // 0
SUB EDX, 32
JNS @@4
FADDP ST(3),ST // ST(3) := ST + ST(3); Pop ST
FADD // ST(1) := ST + ST(1); Pop ST
FADD // ST(1) := ST + ST(1); Pop ST
FWAIT
end;
|
But under SmartMS, Assembler is pure fun – because to a browser assembler is JavaScript!
See more about ASM Block.
Example code : Calculate the mean of a set of 3 numbers using Smart Mobile Studio
function Mean(const Data: array of Float): Float;
var thisAverage : Variant;
begin
//Result := SUM(Data) / (High(Data) - Low(Data) + 1);
asm
var numberArray=[2,13,56], thisTotal=0,thisAverage=0;
// add elements of array together
for(var i=0;i<numberArray.length;i++)
{thisTotal+=numberArray[i];}
// calculate average
@thisAverage=(thisTotal/numberArray.length);
end;
// display result
Result := thisAverage;
end;
function Average(a, b, c : Float) : Float;
begin
// return the average of the 3 passed numbers
Result := Mean([a, b, c]);
end;
procedure TForm1.btnClearClick(Sender: TObject);
begin
// Let us call this function
ShowMessage(Format('Average of 2, 13 and 56 = %f',[Average(2,13,56)]));
end;
|
Result is: Average of 2, 13 and 56 = 23.67
function Mean(Data) {
var Result = 0;
var thisAverage = undefined;
var numberArray=[2,13,56], thisTotal=0,thisAverage=0;
// add elements of array together
for(var i=0;i<numberArray.length;i++)
{thisTotal+=numberArray[i];}
// calculate average
thisAverage=(thisTotal/numberArray.length);
Result = Number(thisAverage);
return Result
};
function Average$1(a$56, b$18, c) {
return Mean(([a$56,b$18,c].slice()));
};
|
VARIABLE BY VALUE
In our example above, we declared an integer variable for use in a calculation by the function.
Subroutines can have their own types, constants and variables, and these remain local to the routine.
Variable values are reset every time the routine is called (use a class object to hold onto data across routine calls).
Here is an illustration of this local variable action using SmartMS.
procedure DoIt(A : Integer);
begin
A := A * 2;
ShowMessage(Format('A in the procedure = %d',[A]));
end;
procedure TForm1.InitializeForm;
var
A : Integer;
begin
inherited;
// this is a good place to initialize components
A := 22;
ShowMessage(Format('A in program before call = %d',[A]));
// Call the procedure
DoIt(A);
ShowMessage(Format('A in program now = %d',[A]));
end;
|
The result is:
A in program before call = 22
A in the procedure = 44
A in program now = 22
------------------------------
The procedure is passed A, updates it and displays it. The caller then displays the A that it passed to the procedure.
It is unchanged. The procedure sees this A as if it were defined as a local variable.
Like local variables, when the procedure ends, their value is lost.
VARIABLE BY REFERENCE
The default was of passing data is by what is called by value. Literally, the parameter value is passed to the subroutine argument.
reference to the argument is then to this copy of the variable value.
Passing by reference means that the subroutine actually refers to the passed variable rather than its value.
Any changes to the value will affect the caller variable. We declare a variable to be passed by reference with the var prefix.
Rewriting the above code to use by reference changes matters:
Code example: Here is an illustration of passing data by reference SmartMS.
procedure DoIt(var A : Integer);
begin
A := A * 2;
ShowMessage(Format('A in the procedure = %d',[A]));
end;
procedure TForm1.InitializeForm;
var
A : Integer;
begin
inherited;
// this is a good place to initialize components
A := 22;
ShowMessage(Format('A in program before call = %d',[A]));
// Call the procedure
DoIt(A);
ShowMessage(Format('A in program now = %d',[A]));
end;
|
The result is:
A in program before call = 22
A in the procedure = 44
A in program now = 44
------------------------------
Now the caller A variable is updated by the procedure.
This is a very useful way of returning data from a procedure. It also allows us to return more than one value from a subroutine.
Constant value parameters
For code clarity, and performance, it is often wise to declare arguments that are only ever read by a subroutine as constants. This is done with the const prefix. It can be used even when a non-constant parameter is passed. It simply means that the parameter is only ever read by the subroutine.
procedure DoIt(Const A : Integer; var B : Integer);
begin
B := A * 2;
ShowMessage(Format('A in the procedure = %d',[A]));
end;
procedure TForm1.InitializeForm;
var
A,B : Integer;
begin
inherited;
// this is a good place to initialize components
A := 22;
// B is not defined here
// Call the procedure
DoIt(A,B);
ShowMessage(Format('B has been set to = %d',[B]));
end;
|
Result is: B has been set to 44
Notice that when defining two argument types, the arguments are separated with a ;.
function InitializeForm(Self) {
var A$9 = 0;
TW3CustomForm.InitializeForm(Self);
A$9 = 22;
alert(("A in program before call = "+A$9.toString()));
DoIt(A$9);
alert(("A in program now = "+A$10.toString()));
}
|
function InitializeForm(Self) {
var A$9 = {};
A$9.v = 0;
TW3CustomForm.InitializeForm(Self);
A$9.v = 22;
alert(("A in program before call = "+A$9.v.toString()));
DoIt(A$9);
alert(("A in program now = "+A$9.v.toString()));
}
|
function InitializeForm(Self) {
var A$9 = 0;
var B$3 = {};
B$3.v = 0;
TW3CustomForm.InitializeForm(Self);
A$9 = 22;
DoIt(A$9,B$3);
alert(("B has been set to = "+B$3.v.toString()));
}
|
Same routine, different parameters
One of the benefits of Object Oriented programming is that some of the rigidity of procedural languages was relaxed. This has spilled over into non object orientation subroutines (as opposed to class methods).
One of the benefits is that we can define two or more subroutines that have exactly the same name. SmartMS is able to tell them apart by the different number or types of parameters.
The example below illustrates this with two versions of the DoIt procedure using SmartMS.
procedure DoIt; overload;
begin
ShowMessage('DoIt with no parameters called');
end;
procedure DoIt(msg : String); overload;
begin
ShowMessage('DoIt called with parameter : '+msg);
end;
procedure TForm1.InitializeForm;
begin
inherited;
// Call the procedure using no parameters
DoIt;
// Now call the procedure using one parameter
DoIt('Hi there');
end;
|
Result is:
DoIt with no parameters called
DoIt called with parameter : Hi There
function DoIt() {
alert("DoIt with no parameters called");
};
function DoIt$1(msg$1) {
alert("DoIt called with parameter : "+msg$1);
};
|
|