Show/Hide Toolbars

XSharp

Purpose

Declare a local function

Syntax

 [Modifiers] LOCAL FUNCTION <idFunction>

 [Typeparameters]

 [([<idParam> [AS | REF|OUT|IN <idType>] [, ...])]

 [AS <idType>]

 [TypeparameterConstraints]

 [=> <expression>]

 CRLF

 [<Body>]

 END FUNCTION

Arguments

[Modifiers]The only valid modifiers for a local function are UNSAFE and/or ASYNC

 

<idFunction>A valid identifier name for the function.  A function is an entity and, as such, shares the same name space as other entities.  This means that it is not possible to have a function and a class, for example, with the same name.

 

TypeParametersThis is supported for methods with generic type arguments. This something like <T> for a method with one type parameter named T. Usually one of the parameters in the parameter list is then also of type T.

 

<idParam>A  parameter variable.  A variable specified in this manner is automatically declared local.  These variables, also called formal parameters, are used to receive arguments that you pass when you call the entity.

 

AS | REF|OUT|IN <idType>Specifies the data type of the parameter variable (called strong typing).  AS indicates that the parameter must be passed by value, and REF indicates that it must be passed by reference with the @ operator. OUT is a special kind of REF parameter that does not have to be assigned before the call and must be assigned inside the body of the entity. IN parameters are passed as READONLY references.
The last parameter in the list can also be declared as PARAMS <idType>[] which will tell the compiler that the function/method may receive zero or more optional parameters.
Functions or Methods of the CLIPPER calling convention are compiled to a function with a single parameter that this declared as Args PARAMS USUAL[]
 

 

AS <idType>Specifies the data type.  If omitted, then depending on the compiler options the type will be either USUAL or determined by the compiler.

 

TypeParameterConstraintsHere you can specify constraints for the Type parameters, such as WHERE T IS SomeName or WHERE T IS New

 

=> <Expression>Single expression that replaces the multiline body for the entity. CANNOT be compiled with a body

 

<Body>Program statements that form the code of this entity.
The <Body> can contain one or more RETURN statements to return control to the calling routine and to serve as the function return value.  If no return statement is specified, control passes back to the calling routine when the function definition ends, and the function will return a default value depending on the return value data type specified (NIL if the return value is not strongly typed).
CANNOT be combined with an Expression Body

 

 

END FUNCTIONThese mandatory keywords indicate the logical end of the function.

Description

A local function is defined as a nested function inside a containing member. The END FUNCTION is mandatory so the compiler knows where the function ends and its surrounding container continues.

In the example below the WAIT command is part of the Start() function and will be executed after result of the call to Fact() is shown.

Example

FUNCTION Start AS VOID
   ? Fact(10)
  LOCAL FUNCTION Fact(nNum AS LONG) AS LONG
      IF nNum == 1
          RETURN 1
      ENDIF
      RETURN nNum * Fact(nNum-1)    
  END FUNCTION
  WAIT                                  
  RETURN

 

See Also

FIELD, LOCAL, MEMVAR, METHOD, PROCEDURE, RETURN, FUNCTION