Home

Functions Fundamentals

 

Introduction to Functions

 

Overview

A function is a section of code that holds a value and may performs an action to produce a value or a result.

Creating a Function

The fundamental formula used to create a function is:

let FunctionName = Function Body

You start with the let keyword, followed by a name for the function. The name follows the rules we saw for names of variables. By tradition, the name of a function conveys an action. Here is an example that starts a function:

let display . . .

Defining a Function

Defining or implementing a function is equivalent to specifying its purpose. The definition or implementation of a function is done in its body, which is after the = sign. If the purpose of the function is short, the body can directly follow the = sign. Here is an example where the body of a function simply holds a string:

let something = "Employees Records"

If the purpose of the function is long, it may need various lines of code. In this case, the body must start on a line after the = sign. In this case also, the body of the function must be indented. The indentation can be created by simply pressing the Space bar from the level of the let keyword used on the function. The indentation is controlled by the Tabs section of the F# part of the Text Editor node in the Options dialog box (Tools -> Options):

The Options Dialog Box

To make your code better to read, indentation is typically applied by using 2 or 4 spaces (some of us typically press the Tab key). The formula to create the function would be:

let FunctionName =
    Function Body

Here is an example:

let something =
    "Employee Record"

Normally, you can do anything you want in the body of a function. For example, if necessary, you can declare one or more variables and use them as you see fit. The variables declared in a function are referred to as local variables. Here are examples of local variables:

let display =
    let sentence = "We need to talk!"
    let number = 24
    let result = "About what?"

The variables can also be of class types. They can also be of the types of Windows controls. Here is an example:

open System
open System.Windows.Forms

let create() =
    let exercise = new Form()

You can perform more actions than simply declaring variables.

In the body of a function, if you use code that leads to dependent lines, those dependent lines must be indented.

The Body of a Function

The compiler considers that the section after the = sign is the body of the function. If the body of the function spans many line and you want to explicitly indicate where that body starts and where it ends, include it between the begin keyword and the end keyword. The lines between those two keywords must be indented. Here is an example:

let show message =
    begin
	sprintfn "%s" message
    end

Also remember that you can declare local variables in the function and use them as necessary. Here is an example:

let calculate (x:int) (y:int) (z:int) =
    let addition = x + y + y
    sprintf "%d + %d + %d = %d" x y z addition

Calling a Function

After creating a function, to see its result, you must call it. If you create the functions as we have done so far, there is nothing to do: the function would be called automatically.

As an alternative, if you want to explicitly call a function, when creating it, add some empty parentheses. Then, to call the function, type its name. Here is an example:

open System
open System.Windows.Forms

let create() = 
    let exercise = new Form()
    exercise.Text <- "Graphical User Interface"

    Application.Run(exercise)

create

If the function was created with empty parentheses, then you must follow its name with empty parantheses. Here is an example:

open System
open System.Windows.Forms

let create() = 
    let exercise = new Form()
    exercise.Text <- "Graphical User Interface"

    Application.Run(exercise)

create()

This would produce:

Introduction to Functions

Parameters and Arguments

 

Introduction

To perform its actions, a function may need some values. These are called parameters and they must be supplied to the function. The formula to create such a function is:

let FunctionName(parameter) = Function Body

In this case, after the name of the function, add a parameter represented by a name. Here is an example:

let calculate(x) . . .

You don't have to use parentheses. You can just add a space after the name of the function and the parameter. Here is an example:

let calculate x . . .

To define or implement the function, after the parameter or the list of parameters and the assignment operator "=", in the body section, use the parameter as you see fit. Once again, remember that if a function is short enough, you can define it on the same line as the function name. If the function is long, start its body in the lines after = and indent the code. Here is an example:

let add x = x + x

This is the same as this:

let add x = x + x // The parameter is not included in parentheses

Remember that you can declare one or more local variables in the function and use it/them as necessary. Here is an example:

let create(title) = 
    let exercise = new Form()
    exercise.Text <- title

    Application.Run(exercise)

Calling a Function With Parameters

To call a function that takes a parameter, you must supply a value for the parameter. This is referred to as passing the argument to the function. To do this, after the name of the function, add a space and the argument or the list of arguments. Here is an example:

open System
open System.Windows.Forms

let create title = 
    let exercise = new Form()
    exercise.Text <- title

    Application.Run(exercise)

create("Application Development Environment")

This would produce:

Introduction to Functions

When calling a function that takes at least one parameter, you don't have to add its parameter(s) in parentheses. Here are examples:

open System
open System.Windows.Forms

let create title = 
    let exercise = new Form()
    exercise.Text <- title

    Application.Run(exercise)

create "Application Development Environment"
     
 

The Returned Value of a Function

The return value of a function is the value the function produces after performing its action(s). Because F# is an inferred language, you can create a function without specifying its return value. Otherwise, the return value of a function is the last expression it presents. Consider the following example:

let calculatePayrol salary =
    salary * 40

If the body of the function is on the same line with the name of the function, its return value is the last expression on the line.

 

Introduction to Built-In Functions

 

Introduction

A function is referred to as built-in if it is already created and you can just use it directly in your code. The F# language includes many built-in functions. The .NET Framework also includes functionality for built-in functions and it includes a few (normally, the .NET Framework doesn't use functions, which may have something to do with the C# language which doesn't use functions either).

If the built-in functions of the F# language are not enough, probably the next best library to use is that of the Visual Basic language, which includes one of the biggest and best libraries of functions. The Win32 library, which is the original library of the Microsoft Windows, holds the foundational functionality of the operating system, including a large library of functions. You can use those functions in your program. If the functionality you are looking for is still not available from F#, Visual Basic, Win32, or the .NET Framework, you can check around for commercial libraries.

You can directly use a function that is built in the F# language. To use functionality that is part of anything outside of F#, you must first add its reference in your program.

Conversion Functions

A conversion function is a function used to convert a value from one type into another type. Both the F# language and the .NET Framework support value conversion.

F# provides various functions for value conversion. There is a function for each type. The basic formula to follow is:

identifier = conversion-function value-or-variable

identifier is what will receive the result. It can be a variable, in which case the formula to use would be:

let variable-name = conversion-function value-or-variable

conversion-function is the function to use. We will review the available functions. The last part, value-or-variable, is a value or the name of a vaible that holds the value to convert.

   
   
 

Home Copyright © 2009-2015, FunctionX Next