Previous Copyright © 2015 FunctionX Next


The Entry Point of a Program



Many computer languages, especially compiled languages like C, C++, Visual Basic (the original BASIC language doesn't apply here), Java, and C# have a special function named main (or Main) that controls where a program starts executing. That function, or the section where it is defined, is called an entry point. Many other languages, especially interpreted languages (like JavaScript, VBScript, etc) don't have that function.

The F# language doesn't directly support the entry point concept. In fact, F# proceeds from the top of the document that contains the code (of a program) to the bottom section of the document. To specify an entry point for an F# program, somewhere in the document that holds the code of the application, create a section that starts with an attribute named EntryPoint. This would be done as follows:

. . .

On the line(s) after the [<EntryPoint>] attribute, you can specify what to do. If you want to specify the starting point of execution of the program, you should create a function named main and that takes one argument (the argument will actually be treated as a group of values, called an array). In the body of the function, do whatever you want. Before exiting the function, you should indicate a constant integer as its return value. Normally, you can use any number. Here is an example:

open System
open System.Windows.Forms

let exercise : Form = new Form()

let main arguments =
    Application.Run exercise

By tradition, the return value of the entry point function is specified as 0.

Home Copyright © 2009-2015 FunctionX Home