unit UnitMain; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, TypInfo; //Constants Const MAX_PLAYERS = 10; type { TFormMain } TFormMain = class(TForm) ButtonStart: TButton; Memo: TMemo; procedure ButtonStartClick(Sender: TObject); private { private declarations } public { public declarations } end; var FormMain: TFormMain; GlobalInteger : integer; //A global Variable declaration implementation {$R *.lfm} { TFormMain } procedure DuplicateProcedure(v:integer); var LocalResult:integer; //Local variable begin v:=2*v; //Local variable scope ! LocalResult:=v; FormMain.Memo.Lines.Add('The procedure tells : The local result is : ' + inttostr(LocalResult)); end; function DuplicateFunction(v:integer):integer; //response var LocalResult:integer; //Local variable begin v:=2*v; //Local variable scope ! LocalResult:=v; result := LocalResult; end; procedure DuplicateProcedureGlobalVariable(); begin GlobalInteger:=2*GlobalInteger;; FormMain.Memo.Lines.Add('The procedure tells : The local result is : ' + inttostr(GlobalInteger)); end; //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// //MAIN PROGRAM procedure TFormMain.ButtonStartClick(Sender: TObject); var LocalInteger : integer; //Local variables declarations S1,S2 : string; i : integer; Players : array [1..9] of string; //Static array UnlimitedPlayers : array of string; //Dynamic array begin //Comments//////////////////////////////////////////////////////////// //Comment single line { Comment Multi line ... } //Constants/////////////////////////////////////////////////////////// //MAX_PLAYERS := 11; //THIS IS INCORRECT You can not assign a constant //Variables/////////////////////////////////////////////////////////// //Inizialization (assigning variables) LocalInteger := 0; S1 := 'String 1'; S2 := 'String 2'; //Print integer Memo.Lines.Add('Your local integer is : ' + inttostr(LocalInteger)); Memo.Lines.Add('Your constant value of max players is : ' + inttostr(MAX_PLAYERS)); //Strings concatenation Memo.Lines.Add(S1 + ' , ' + S2); //If then else//////////////////////////////////////////////////////// Randomize; i:=Random(100); if i<30 then begin Memo.Lines.Add('The number ' + inttostr(i) + ' is small'); end else if (i>=30) and (i<60) then begin Memo.Lines.Add('The number ' + inttostr(i) + ' is medium'); end else begin Memo.Lines.Add('The number ' + inttostr(i) + ' is big'); end; //Loops/////////////////////////////////////////////////////////////// //For Loop for i:=0 to 10 do begin LocalInteger:=LocalInteger + 1; end; Memo.Lines.Add('Incremented with for. Your integer is now : ' + inttostr(LocalInteger)); //Will be 11 for i:=10 downto 0 do begin LocalInteger:=LocalInteger - 1; end; Memo.Lines.Add('Decremented with for. Your integer is now : ' + inttostr(LocalInteger)); //Will be 0 //While loop While i <= 10 do begin LocalInteger:=LocalInteger + 1; i := i + 1; // Increment the number end; Memo.Lines.Add('Incremented with while. Your integer is now : ' + inttostr(LocalInteger)); //Will be 11 //Procedure & Function /////////////////////////////////////////////// //This is a procedure call . (I do not get a response) DuplicateProcedure(LocalInteger); //Will get 22 (11*2) //This is a function call. (I do get a response) Memo.Lines.Add('The function produced : ' + inttostr(DuplicateFunction(LocalInteger))); //Will get 22 (11*2) ///////////////////////////////////////////////////////////////////// //Scopes //Local Scope: As you can see the v and LocalResult local variable // which is located internally // at the Duplicate Procedure/Function // dies after the end of the procedure / Function. DuplicateProcedure(LocalInteger); //Will get 22 (11*2) Memo.Lines.Add('The function produced : ' + inttostr(DuplicateFunction(LocalInteger)) ); //Will get 22 (11*2) Memo.Lines.Add('The local scope demonstration : ' + inttostr(LocalInteger)); //Will be 11 //Global Scope : As you can see in this case we do not pass a variable // since it is a global variable // The global variable changed internally // from the Procedure/Function // will keep its value and after // the procedure/function execution. GlobalInteger:=40; DuplicateProcedureGlobalVariable(); //Will get 80 Memo.Lines.Add('The global scope demonstration : ' + inttostr(GlobalInteger)); //80 will remain ////////////////////////////////////////////////////////////////////// //arrays //1) Fixed arrays //a) Assign an array of players for i:=1 to 9 do begin Players[i]:='Player number' + inttostr(i); end; //b) Read array Memo.Lines.Add(Players[2]); //2)Dynamic arrays //a) Assign a dynamic array //Increase the array by one element SetLength(UnlimitedPlayers, Length(UnlimitedPlayers)+1 ); //The first element at a dynamic array is at index 0 UnlimitedPlayers[0]:= 'First player'; //Increase the array by two elements SetLength(UnlimitedPlayers, Length(UnlimitedPlayers)+2 ); UnlimitedPlayers[1]:= 'Second player'; UnlimitedPlayers[2]:= 'Third player'; //b) Read array Memo.Lines.Add(UnlimitedPlayers[0]); Memo.Lines.Add(UnlimitedPlayers[1]); Memo.Lines.Add(UnlimitedPlayers[2]); //c) Remove last element SetLength(UnlimitedPlayers, Length(UnlimitedPlayers)-1 ); //d) Read array again Memo.Lines.Add(UnlimitedPlayers[0]); Memo.Lines.Add(UnlimitedPlayers[1]); //Memo.Lines.Add(UnlimitedPlayers[2]); // this element is deleted end; end.
That's all for now.
In next article we'll complete the example of introduction.
Have fun!