Using Functions in Smart Pascal
A function (like a procedure) is a named sequence of statements but it returns a value. This means that you can usually use a function by name as if it were a variable. (However, unlike a variable, a function cannot be used to the left of the assignment operator := because values cannot be assigned to it). You will have seen the inbuilt function IntToStr used instead of a variable in the display of the frame rate.
Note that you must give the return type of the function after a colon in the function heading. The value to be returned by the function is assigned to the inbuilt local variable with identifier Result.
This example, using a function to randomly select a string from an array, performs the same task as the procedure with a var parameter on the previous page.
We developed both examples on this page with Version 2.2 of Smart Mobile Studio. In each case, change the line Canvas.Font := '40pt verdana'; to Canvas.FontStyle := '40pt verdana'; when using Version 3.0
unit Unit1; interface uses System.Types, SmartCL.System, SmartCL.Components, SmartCL.Application, SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics; type TCanvasProject = class(TW3CustomGameApplication) protected procedure ApplicationStarting; override; procedure ApplicationClosing; override; procedure PaintView(Canvas: TW3Canvas); override; end; implementation var FontColour, BackColour: string; function RandomColour : string; var Colours : array [0 .. 5] of string = ['red', 'pink', 'white', 'black', 'blue', 'green']; begin Result := Colours[RandomInt(6)]; end; procedure TCanvasProject.ApplicationStarting; begin inherited; Randomize; FontColour := RandomColour; repeat BackColour := RandomColour; until BackColour <> FontColour; GameView.Delay := 20; GameView.StartSession(True); end; procedure TCanvasProject.ApplicationClosing; begin GameView.EndSession; inherited; end; procedure TCanvasProject.PaintView(Canvas: TW3Canvas); begin // Clear background Canvas.FillStyle := BackColour; Canvas.FillRectF(0, 0, GameView.Width, GameView.Height); // Draw framerate on screen Canvas.Font := '40pt verdana'; Canvas.FillStyle := FontColour; Canvas.FillTextF('FPS: ' + IntToStr(GameView.FrameRate), 10, 50, MAX_INT); end; end.
Array Parameters
You pass parameters to functions in the same way as to variables. We have given examples of passing integers and strings to procedures, and now cover the passing of arrays. The most versatile way is to pass a dynamic array, as in this demonstration. The inbuilt type TStrArray is defined as an array of string.
unit Unit1; interface uses System.Types, SmartCL.System, SmartCL.Components, SmartCL.Application, SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics; type TCanvasProject = class(TW3CustomGameApplication) protected procedure ApplicationStarting; override; procedure ApplicationClosing; override; procedure PaintView(Canvas: TW3Canvas); override; end; implementation var FontColour, BackColour: string; BackColours: TStrArray = ['red', 'pink', 'white', 'black', 'blue', 'green']; FontColours: TStrArray = ['aqua', 'fuchsia', 'gray', 'lime', 'maroon', 'navy', 'olive', 'purple', 'silver', 'teal', 'yellow' ]; function RandomString(Strings : TStrArray): string; begin Result := Strings[RandomInt(Strings.High + 1)]; end; procedure TCanvasProject.ApplicationStarting; begin inherited; Randomize; FontColour := RandomString(FontColours); BackColour := RandomString(BackColours); GameView.Delay := 20; GameView.StartSession(True); end; procedure TCanvasProject.ApplicationClosing; begin GameView.EndSession; inherited; end; procedure TCanvasProject.PaintView(Canvas: TW3Canvas); begin // Clear background Canvas.FillStyle := BackColour; Canvas.FillRectF(0, 0, GameView.Width, GameView.Height); // Draw framerate on screen Canvas.Font := '40pt verdana'; Canvas.FillStyle := FontColour; Canvas.FillTextF('FPS:' + IntToStr(GameView.FrameRate), 10, 50, MAX_INT); end; end.