F# Example: Hello World
In F# programming language, a simple “hello world” program can be written by multiple ways.
Let’s see the top 4 ways to create a simple F# example:
- Simple Example using printfn
- using System.Console
- using open System
- using Class
- using Function
F# Simple Example using printfn
Output:
Hello World!
Here, printfn is a function that prints formatted output on the console.
F# Example: using System.Console
We can write the first F# program using System.Console.WriteLine() method like C#. Here, System is a namespace, Console is a class and WriteLine is a method.
Output:
Hello World
F# Example: using open System
If we write open System, it means we don’t need to specify System namespace for accessing any class of this namespace like Console etc. Here, we are using Console class without specifying System.Console.
Output:
Hello World
F# Example: using class
Output:
Hello World!
Description
type: It is a keyword which is used to define type in F#.
Program: It is a name of type class. A class is a blueprint or template from which objects are created. It can have data members and methods.
class: It is a keyword which is used to create class type. It is optional.
do: It is a keyword which is used to execute independent code.
printf : It is a function which is used to produce formatted output.
end: It is used to end declaration of a class. It is optional.
new: It is a keyword which is used to create object for the specified class.
F# Example: using function
Output:
Hello World!
Description
let: Here, let is a keyword which is used to declare variable or function.
program: It is a name of function.
printf: It is a function which is used to produce formatted output.
program: The last “program” statement is used to call program function.