I am total newbie in pascal, started from zero 2 days ago so if somebody could provide the solution, I could analyze it and learn/understand how its done.
Basically what I am trying to do:
I have two file called "Start.pas" and "ReusableFunctions.pas". They both located in same folder.
In Start.pas I call a procedure "FunctionOne();" that is located in ReusableFunctions.pas
I want to pass variable "ToTake" from Start.pas to that procedure in ReusableFunctions.pas, so that procedure prints correct response.
The problem is I don't know how to pass variable, spend 5h searching trying this and that, cant make it to work. Feel so silly and sleepy...
My Start.pas file code:
USES
SysUtils, Classes, ReusableFunctions;
VAR
ToTake: integer = 1;
BEGIN
FunctionOne();
END.
My ReusableFunctions.pas code:
UNIT ReusableFunctions;
INTERFACE
USES
SysUtils, Classes;
PROCEDURE
FunctionOne();
IMPLEMENTATION
procedure FunctionOne();
begin
begin
case ToTake of
1: Print('A');
2: Print('B');
3: Print('C');
4: Print('D');
5: Print('E');
else Print('ERROR');
end;
end;
END.
It's quite simple: if you want your function to have parameters, declare them. Any programming textbook, for Pascal or most other languages, will teach you about parameters (or arguments, as they are called in some other languages). They are crucial to the proces of programming.
In unit ReusableFunctions, declare:
procedure FunctionOne(Decision: Integer);
implementation
procedure FunctionOne(Decision: Integer);
begin
case Decision of
1: Print('A');
2: Print('B');
3: Print('C');
4: Print('D');
5: Print('E');
else
Print('ERROR');
end;
end;
Now you can call it from the main program like:
FunctionOne(ToTake);
Note that the name of a parameter does not have to be the same as the variable or value passed to it. I called the parameter Decision
, and that is how it is known inside the function (or, as a programmer would say: in the scope of the function).
FWIW, there are very good Pascal textbooks online. Take a look at e.g. Marco Cantù's online book Essential Pascal.