Custom Procedures and functions
These are procedures and functions not already included in the include you are using. AKA srl-6, AeroLib , and so on.
In this article I will show you how to set up custom procedures and function, along with using default parameters.
Just think of a parameter as a required bit of code, that is needed for the function or procedure to work.
1: Custom Procedures
To start this off, we will be creating a custom writeln procedure that requires a parameter.
program new;
procedure DebugString(text:string);
begin
writeln(text);
end;
begin
DebugString('Hi this is a string')
end.
In the code above, we create a procedure called DebugString with the parameter of text as string.
Then to use that parameter, we use the writeln function to write text.
Down in the main loop, we use that procedure to write the line 'Hi this is a string', which sets the text var for the procedure to work.
Result:
It simply writes the line we specified as text.
2: Default Parameters
To use a default parameter, we are goin to adjust the code a bit.
program new;
procedure DebugString(text:string; sometext:string = ' and this is a string too');
begin
writeln(text,sometext);
end;
begin
DebugString('Hi this is a string')
end.
We changed the code by:
- Adding one more parameter to the procedure debug string, to do this we just add a semi-colon after the parameter.
- Then we add the parameter sometext as a string and set it to equal ' and this is a string too'.
- The last thing we do is, use the write line function to write both texts.
Results:
Hmm but what if you want to change the some text string? Well that easy too.
DebugString('Hi this is a string',' and so is this')
We just add it to the debug string procedure.
Result:
3: Custom Functions
Now that we know how to use default parameters and create custom procedures, lets check out how to make custome functions.
We are going to create a simple calculator function.
program new;
function calculator(m,w:integer):integer;
begin
result:= m*w;
end;
begin
writeln(calculator(5,10))
end.
We create a function called calculator with the parameters m and w as intergers, notice we can specify the same type of variable by simply adding a comma after each then defining it.
Then simba must know what the function is returning in this case it is an interger.
So with in this function we say the result is m * w.
Then down in the main loop we write the line calculator and the parameters of 5 and 10.
Result:
You could even add in some default or optional parameters if you wish.