﻿# User-Defined Functions

_Updated October 2016; see History_

**\{PRIVATE\} \{DYNAMIC\} FUNCTION FN'name\{\$\}(p1\{=defval\} \{as typsiz\{:mod\}\},...pN...) \{as typsiz\}**

<optional local MAP statements, with optional STATIC prefix>

<optional XGETARGS>

<nearly any kind of statements>

**     FN'name = <expression> **   \! set return value

**     <optional XPUTARGs>**



**ENDFUNCTION**

The function definition, as shown above, starts with the zero or one of the optional keywords "PRIVATE" / "DYNAMIC" (see [Private Functions and Procedures](privatefunctionsandproc.md) and [Dynamic Functions](dynamicfunctions.md) for details), followed by the keyword "FUNCTION" followed by the function name, the list of formal parameters, and the function return type. The function name by convention starts with **FN'**, **Fn'**, or **fn'** so that references embedded in expressions can easily be identified as function calls rather than arrays; see [Function Name and Type](functionnameandtype.md) for clarification. The function name must end with **\$** if the function returns a string or unformatted or defined structure type. If the final \{_as typsiz_\} clause (which defines the return type of the function) is omitted, the default type is **F6**.  

The _typsiz_ (data type and size) specification is similar to that used in MAP statements, except without a comma between the type code and the size. For example:

FUNCTION Fn'Test(p1 as S10, p2 as f6, p3 as B2, p4 As x256) AS i2



Function fn'display'name\$(cus as ST\_CUS\_REC, bold as BOOLEAN) as s MAX\_NAME



function fn'test(p1 as s20:inputonly, p2 as ST\_PHONE:outputonly)



The first example above illustrates that, as with virtually all keywords, the FUNCTION keyword and_ AS typsiz _clauses are **not** case sensitive. However, as with all other variables and labels, the function name and the formal parameter names **are** case sensitive, unless compiled with the /ci option.

The optional \{=_defval_\} clause assigns a default value to the parameter; see Named Parameters.

The optional :_mod_ clause may be used to declare certain modifying attributes for the parameter, such as _inputonly _or _outputonly_; see Parameter Type and Size.

**See Also**

•	[XFUNC()](xfunc().md)

•	[Procedures](procedures.md)

•	[SOSLIB:\[907,10\]](https://bitbucket.org/microsabio/soslib/src/master/907010/?at=default) also known as SOSFUNC

**History**

2016 August, A-Shell 6.3.1523, compiler edit 770:  Add private attribute for functions and procedures. 

2014 October, A-Shell 6.1.1391:  Function name does not need to begin with FN'; see History note in [Function Name and Type](functionnameandtype.md) 

2014 September, A-Shell 6.1.1389:  A-Shell now supports [Passing DIMX Arrays](passingdimxarrays.md) to functions/procedures by reference. 

2014 January, A-Shell 6.1.1373:  [Named parameters](errortrappinginfunctions.md) implemented in A-Shell. 

**Subtopics**

- [Function Name and Type](functionnameandtype.md)

- [Parameter Type and Size](parametertypeandsize.md)

- [Named Parameters](namedparameters.md)

- [Parameter Passing](parameter-passing.md)

- [Return Expression](returnexpression.md)

- [Local Variables](localvariables.md)

- [Global/External Variables](globalexternalvariables.md)

- [Exit Label \$EXIT](exit_label_$exit.md)

- [Error Trapping in Functions](errortrappinginfunctions.md)

- [Invoking A Function](invokingafunction.md)

- [Private Functions and Procedures](privatefunctionsandproc.md)

- [Dynamic Functions](dynamicfunctions.md)

- [Ellipsis Function Arguments](ellipsisfunctionarguments.md)

- [Indirect Function Calls](indirectfunctioncalls.md)

- [User-Defined Function Examples](user-definedfunctionexamples.md)

- [Implicit Function Return Value Capture](implicitfunctionreturn.md)