---
url: >-
  /statelake/scripting-reference/functions-and-procedures/what-is-a-function/defining-or-declaring-a-function.md
---

# Defining Or Declaring A Function

When declaring a user-defined function, the function heading specifies the identifier (name) for the function, a list of the formal parameters (if any) within parentheses, and the function result type. Parameters can also be referred to as Arguments.

Function declarations can be thought of as small multi-line programs. The first line is always prefixed by the keyword **Function**, followed by a list of the parameters that are expected to be input, and lastly, the type for the single output result. For a simple function, the subsequent lines contain the statement block – the body of the function that specifies how the function actually behaves. Statement blocks start with the keyword **begin**, and finish with the keyword **end**.

Please also refer to [User-defined Functions and Procedures](/statelake/scripting-reference/functions-and-procedures/what-is-a-procedure/user-defined-procedures).

Functions will always return a result value, as separate from any of the arguments. The data type of the result is specified in the declaration of the function, such as **String** below -

MyHowColdFunction( Arg1: integer) : string;

The following simple example declares the user-defined function called **MyFunc**. The function has a single parameter called **MyArg** which is a piece of data that has been given a name. This is called a named pair, a couplet, or a type pair. **MyArg** is defined as a string, and in this case the result will also be a string. But the parameter types and result type do not have to be the same.

The generated output will be assigned to the system variable known as **Result**. The statement block instructions say that the **Result** will be a new string by taking the value submitted by **MyArg**, prefixing it with three asterisks, and then place three asterisks as a suffix with no blank spaces between.

```pascal
function MyFunc(MyArg:string):string;
begin
  Result := '***'+MyArg+'***';
end;
```

Or such as this example with the user-defined function **WhatTemp**, which takes temperature value in degrees fahrenheit as the parameter, and converts it to the equivalent degrees celsius by way of a calculation.

```pascal
function WhatTemp(FahTemp:double):double;
begin
  Result := (((FahTemp-32.00)/9.00)*5.00);
end;
```

A very useful user-defined function called **BooleanAsString** takes a **boolean** result, and converts it to a **String**, as shown below. The function can then be called within your script. Please see [Testing For Null (empty or blank) Values](/statelake/scripting-reference/data-types/testing-for-null-empty-or-blank-values) for an example of the use of this particular function.

```pascal
function BooleanAsString(b:boolean):string;
begin
 if b then result := 'true' else result := 'false';
end;
```

If you were declaring a function that required more than one parameter of the same data type, the parameters are separated by a comma, and you can use **parameter shorthand** by declaring the function parameters as follows. The function **TheirMoniker** creates a new string by joining the three string values together with an added space separating the three individual strings.

```pascal
function TheirMoniker(Title,Christian,Surname:string):string;
begin
  Result := Title+' '+Christian+' '+Surname;
end;
```

A function can also of course use multiple parameters of the same type to produce a result with a different data type. The following is a basic example of a function called **TotalCookies** which takes several integer parameters and generates a calculated number that is then inserted into a string. However, to insert the calculated value into the string, it is first converted into a string using the pre-defined function **IntToStr**.

```pascal
function TotalCookies(NumPerBag,BagsPerBox,Boxes:integer):string;
begin
  Result := 'There are '+IntToStr((NumPerBag*BagsPerBox)*Boxes)+' cookies.';
end;
```

Functions can use parameters of mixed data types, but there will always be some sort of conversion of one or more of the types involved. In this example, the function **ContactFunc** is declared using string and integer parameters to generate a string result. Notice the use of a semi-colon separating the two different data type parameters - it is only within the declaration as shown here, that parameters are separated with a semi-colon. This example also includes the integer **HomeNum** which is converted into a string by using the pre-defined function called **IntToStr**. Note the spaces that are intentionally included as part of each typed string.

```pascal
function ContactFunc(CustName:string;HomeNum:integer):string;
begin
  Result := 'Please contact '+CustName+' on '+IntToStr(HomeNum);
end;
```

While these examples all illustrate simple uncomplicated functions, functions can in fact be as complex and as powerful as you need them to be. For instance, if we wanted to generate a function as a daily greeting, we would use a conditional statement block that tested whether the input parameters met certain criteria.

In the following function called **DailyHello**, we pass in a name and birthday date as parameters, and test for whether the birthday date matches today’s date. This function declaration uses several pre-defined functions as well as if-then-else to determine which message is returned as the result. Several comments have also been added, identified by the double slashes at the beginning of the comments. Anything following a double slash is ignored by the system, so comments can be appended to any line as long as the double slashes are used.

```pascal
function DailyHello(StaffName:string;StaffBDay:TDateTime):string;
begin
 //Check whether today's day and month match the birthday date
 If ((DayOf(Now()))=DayOf(StaffBDay)) and (MonthOf(Now())=MonthOf(StaffBDay)) then
 //TODAY is their birthday!
    Result := 'Hello '+StaffName+'. Happy Birthday!'
 else
 //TODAY is NOT their birthday
    Result := 'Hello '+StaffName+'. Have a happy day!';
end;
```
