Difference between revisions of "How to use procedural variables"

From Lazarus wiki
(-> procvar.)
Line 2: Line 2:
  
 
----
 
----
<pre>
+
<delphi>program Test;
program test;
 
  
 
{$mode objfpc}{$H+}
 
{$mode objfpc}{$H+}
Line 10: Line 9:
 
   cthreads,
 
   cthreads,
 
   {$ENDIF}{$ENDIF}
 
   {$ENDIF}{$ENDIF}
   Classes
+
   Classes;
  { add your units here };
+
 
//Make the Types the type corresponds to a function signature
+
// Make the Types the type corresponds to a function signature
 
type
 
type
    TFuncNoArgs_String = function():String;
+
  TFuncNoArgsString = function(): String;
    TFuncOneArgs_String = function(x:string):string;
+
  TFuncOneArgsString = function(x: string): string;
  
//example functions
+
// Example functions
function Hello():String;
+
function Hello: String;
 
begin
 
begin
    result:='Hello There';
+
  Result := 'Hello There';
 
end;
 
end;
function Woah(G:String):String;
+
 
 +
function Woah(G: String): String;
 
begin
 
begin
    result:='Woah ' + G;
+
  Result := 'Woah ' + G;
 
end;
 
end;
  
//overloaded function takes the two types of function
+
// Overloaded function takes the two types of function
//pointers created above
+
// pointers created above
procedure Take(f:TFuncNoArgs_String);overload;
+
procedure Take(f: TFuncNoArgsString); overload;
 
begin
 
begin
    writeln(f());
+
  WriteLn(f());
 
end;
 
end;
  
procedure Take(f:TFuncOneArgs_String);overload;
+
procedure Take(f: TFuncOneArgsString); overload;
 
begin
 
begin
    writeln(f('there!!!'));
+
  WriteLn(f('there!!!'));
 
end;
 
end;
  
 
var
 
var
    ptr:Pointer;
+
  ptr: Pointer;
    list:Tlist;
+
  List: TList;
 
begin
 
begin
// the "@" symbol turns the variable into a pointer.
+
  // the "@" symbol turns the variable into a pointer.
// This must be done in order pass a function as a  
+
  // This must be done in order pass a function as a  
// paramater.  This also demonstrates that pascal
+
  // paramater.  This also demonstrates that pascal
// keeps track of the pointer type so the overloading works!
+
  // keeps track of the pointer type so the overloading works!
 
 
Take(@Hello);
 
Take(@Woah);
 
  
//now put a function in an untyped pointer
+
  Take(@Hello);
ptr:=@Hello;
+
  Take(@Woah);
//Type the pointer and call it all at the same time
 
writeln(TFuncNoArgs_String(ptr));
 
//A Tlist Example
 
list:=TList.create;
 
list.add(@Hello);
 
writeln(TFuncNoArgs_String(list[0]));
 
readln;
 
end.
 
</pre>
 
  
----
+
  // Now put a function in an untyped pointer
 +
  ptr := @Hello;
 +
  // Type the pointer and call it all at the same time
 +
  WriteLn(TFuncNoArgsString(ptr));
 +
  // A TList Example
 +
  List := TList.Create;
 +
  List.Add(@Hello);
 +
  WriteLn(TFuncNoArgsString(List[0]));
 +
  ReadLn;
 +
end.</delphi>

Revision as of 12:25, 31 August 2010

Copy the text below and it will demonstrate the use of procedural variables, this is a fully working program. You don't even need to understand how it works the syntax is pretty simple.


<delphi>program Test;

{$mode objfpc}{$H+} uses

 {$IFDEF UNIX}{$IFDEF UseCThreads}
 cthreads,
 {$ENDIF}{$ENDIF}
 Classes;

// Make the Types the type corresponds to a function signature type

 TFuncNoArgsString = function(): String;
 TFuncOneArgsString = function(x: string): string;

// Example functions function Hello: String; begin

 Result := 'Hello There';

end;

function Woah(G: String): String; begin

 Result := 'Woah ' + G;

end;

// Overloaded function takes the two types of function // pointers created above procedure Take(f: TFuncNoArgsString); overload; begin

 WriteLn(f());

end;

procedure Take(f: TFuncOneArgsString); overload; begin

 WriteLn(f('there!!!'));

end;

var

 ptr: Pointer;
 List: TList;

begin

 // the "@" symbol turns the variable into a pointer.
 // This must be done in order pass a function as a 
 // paramater.  This also demonstrates that pascal
 // keeps track of the pointer type so the overloading works!
 Take(@Hello);
 Take(@Woah);
 // Now put a function in an untyped pointer
 ptr := @Hello;
 // Type the pointer and call it all at the same time
 WriteLn(TFuncNoArgsString(ptr));
 // A TList Example
 List := TList.Create;
 List.Add(@Hello);
 WriteLn(TFuncNoArgsString(List[0]));
 ReadLn;

end.</delphi>