This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
F# is supported in the Visual Studio integrated development environment (IDE).
To begin, ensure that you haveVisual Studio installed with F# support.
One of the most basic projects in Visual Studio is the console app. Here's how to create one:
Open Visual Studio 2019.
On the start window, chooseCreate a new project.
On theCreate a new project page, chooseF# from the Language list.
Choose theConsole App (.NET Core) template, and then chooseNext.
On theConfigure your new project page, enter a name in theProject name box. Then, chooseCreate.
Visual Studio creates the new F# project. You can see it in the Solution Explorer window.
Let's get started by writing some code. Make sure that theProgram.fs file is open, and then replace its contents with the following:
module HelloSquarelet square x = x * x[<EntryPoint>]let main argv = printfn "%d squared is: %d!" 12 (square 12) 0 // Return an integer exit codeThe previous code sample defines a function calledsquare that takes an input namedx and multiplies it by itself. Because F# usesType inference, the type ofx doesn't need to be specified. The F# compiler understands the types where multiplication is valid and assigns a type tox based on howsquare is called. If you hover oversquare, you should see the following:
val square: x: int -> intThis is what is known as the function's type signature. It can be read like this: "Square is a function that takes an integer named x and produces an integer". The compiler gavesquare theint type for now.
Another function,main, is defined, which is decorated with theEntryPoint attribute. This attribute tells the F# compiler that program execution should start there. It follows the same convention as otherC-style programming languages, where command-line arguments can be passed to this function, and an integer code is returned (typically0).
It is in the entry point function,main, that you call thesquare function with an argument of12. The F# compiler then assigns the type ofsquare to beint -> int (that is, a function that takes anint and produces anint). The call toprintfn is a formatted printing function that uses a format string and prints the result (and a new line). The format string, similar to C-style programming languages, has parameters (%d) that correspond to the arguments that are passed to it, in this case,12 and(square 12).
You can run the code and see the results by pressingCtrl+F5. Alternatively, you can choose theDebug >Start Without Debugging from the top-level menu bar. This runs the program without debugging.
The following output prints to the console window that Visual Studio opened:
12 squared is: 144!Congratulations! You've created your first F# project in Visual Studio, written an F# function that calculates and prints a value, and run the project to see the results.
If you haven't already, check out theTour of F#, which covers some of the core features of F#. It provides an overview of some of the capabilities of F# and ample code samples that you can copy into Visual Studio and run.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?