Loading, please wait...

A to Z Full Forms and Acronyms

How to write the first Program with F# in Visual Studio? | F# Tutorial

In this article, you will learn the following:How to create a console application?How to Write the code in F#?How to run the code in F#?

Write the first Program with F# in Visual Studio

In this article, you will learn the following:

  • How to create a console application
  • How to Write the code in F#
  • How to run the code in F#

How to create a console application?

Here are the steps to create a console application:

  1. Open Visual Studio 2019.
  2. Select create a new project on the start window. 
  3. Now, select the F# from the language list on the create a new project page. 
  4. Select the Console App (.NET Core) template, and then select Next. 
  5. On the configure your new project page, you need to write the name of the project in the project name box. Then, select create. 

How to write the code in F#?

Always write code in the Program.fs file and then replace its contents with the following:

F#
let sign square = y*y
let main() =
   Console.WriteLine("sign 7: {0}", (sign 7))

main()

The above code defines a function called square which takes an input y and multiplies it with itself. As we know, F# supports type reference, so we don’t need to mention the type of x. The compiler of F# knows very well the type of multiplication is valid and assigns the same type to the square as x have. The type of the square is int. 

Another function main() is called which is the main function and considered as the EntryPoint attribute. This attribute helps in telling the compiler that the execution of the program should start from there. The same conventions are followed in the other programming languages. 

How to Run the F# Programming code?

The user can run the code and check the outputs by pressing Ctrl+5. Apart from this, you can also select Debug> Start without debugging from the top-level menu bar. This runs the program without even debugging.

A to Z Full Forms and Acronyms