Using Subroutines

From Vectorlab
Jump to: navigation, search

Short introduction to subroutines.

What is a sub-routine


TASK
We need to increment a value and at the same time return the value as string. We need to perform this action for 5 times.

There are always more ways to achieve a task. We present here the different subroutines types while achieving the same result.

  • Use two subroutines of type "Function"
  • Use one subroutine of type "Function" setting a "VAR"


Use two subroutines of type "Function"

The typical function: it can return only one value.

The routine for incrementing:

  • we name the subroutine "IncrementMe"
  • we make it a function: it returns an Integer
  • we let it have a parameter "int" and a one "howManyMore", both accepting an integer
  • add some code to perform the needed actions: increment "int" by howManyMore.
  • "IncrementMe" returns the integer passed in the variable incremented by howManyMore.

The routine for converting to string:

  • we name the subroutine "MakeMeString"
  • we make it a function: it returns a string
  • we let it have a parameter "int" which accepts an integer
  • add some code to perform the needed actions: convert to string
  • "MakeMeString" returns a string from the integer passed in the variable.


PROCEDURE xxxxx;

{ variable declaration }
VAR
	myInteger	: INTEGER;

{ subroutines declaration }
{ returns a value + 1 }
FUNCTION IncrementMe(int, howManyMore: INTEGER): INTEGER;
	BEGIN
		IncrementMe := int + howManyMore;
	END;
	
{ returns the value as string }
FUNCTION MakeMeString(int: INTEGER): STRING;
	BEGIN
		MakeMeString := Concat(int);
	END;

{ main block }
BEGIN
	myInteger := 0;
	WHILE myInteger < 5 DO BEGIN
		myInteger := IncrementMe(myInteger, 1); { here the value increments by one }
		AlrtDialog(MakeMeString(myInteger)); { here the value converts to string }
	END;
	
	(* what happens if you do this instead?
	WHILE myInteger < 5 DO
		AlrtDialog(MakeMeString(IncrementMe(myInteger, 1))); 
		
		{ ENDLESS LOOP! THE VALUE DOESN'T INCREMENT, SO THE WHILE CONDITION NEVER EXITS }
	*)
END;
Run(xxxxx);


Use one subroutine of type "Function" setting a "VAR"

The advanced function: it can return one value and set any number of Variables (labelled with VAR).

  • we name the subroutine "IncrementMe"
  • we make it a function: it returns a string
  • we let it have a parameter "int" which accepts an integer and a parameter "howManyMore" also integer
  • the parameter "int" is declared to be a VAR: any variable employed later while using this routine will be overwritten.
  • add some code to perform the needed actions: increment "int" by "howManyMore" and convert to string.
  • "IncrementMe" returns a string from the incremented integer passed in the variable.


PROCEDURE xxxxx;

{ variable declaration }
VAR
	myInteger	: INTEGER;

{ subroutines declaration }
{ sets a variable its value + 1 and returns the value as string }
FUNCTION IncrementMe(VAR int: INTEGER; howManyMore: INTEGER): STRING;
	BEGIN
		int := int + howManyMore;
		IncrementMe := Concat(int);
	END;

{ main block }
BEGIN
	myInteger := 0;
	WHILE myInteger < 5 DO
		AlrtDialog(IncrementMe(myInteger, 1));
END;
Run(xxxxx);