Использовани функций и процедур в качестве параметров
- Объявить функцию или процедуру, которая будет использована в качестве параметра. В приведенном ниже примере - это TFunctionParameter.
- Определить функцию, которая будет принимать другую функцию в качестве параметра. В приведенном примере - это DynamicFunction.
type TFunctionParameter = function ( const value : integer ) : string ; { ... } function One( const value: integer ): string ; function Two( const value: integer ): string ; function DynamicFunction(f: TFunctionParameter): string ; implementation {$R *.dfm} function One( const value : integer ) : string ; begin result := IntToStr(value div 2 ) ; end ; function Two( const value : integer ) : string ; begin result := IntToStr( 2 * value) ; end ; function DynamicFunction(f : TFunctionParameter) : string ; begin result := f( 1000 ) ; end ; |
Использование:
procedure TForm1 . Button1Click(Sender: TObject); var s: string ; begin s:= DynamicFunction(One); // Возвращает 500 ShowMessage(s); s:= DynamicFunction(Two); // Возвращает 2000 ShowMessage(s); end ; |
Комментарии