Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

Your First F# Program: printfn, Comments and Entry Points

Write your first F# program. Learn printfn and format specifiers, comments, significant whitespace, modules and namespaces, open, and the [<EntryPoint>] main function.

Time to read and write real F#. This lesson walks through a complete program line by line: printing with printfn, writing comments, how indentation replaces braces, how code is organised in modules and namespaces, and how a program starts. Each snippet is read-only F#; a C# stand-in at the end lets you run the same logic here.

Printing with printfn

printfn prints a line. Its first argument is a format string, and the values follow as separate arguments, separated by spaces, not commas:

FSHARP
printfn "Hello, F#!"
printfn "%s is %d years old" "Ada" 36
printfn "Pi is about %.2f" 3.14159
printfn "Is it true? %b" true

Output:

TEXT
Hello, F#!
Ada is 36 years old
Pi is about 3.14
Is it true? true

The format specifiers are checked at compile time. If you write %d but pass a string, the program does not compile. That is one of F#'s small but real safety wins.

Specifier Type
%s string
%d integer
%f, %.2f floating-point (with precision)
%b bool
%c char
%A any value, pretty-printed (great for debugging)
%O any value, using ToString()

printf (no n) prints without a newline. sprintf returns the formatted text as a string instead of printing it, and eprintfn writes to standard error.

FSHARP
let message = sprintf "%s scored %d" "Grace" 98
printfn "%s" message
printfn "%A" [ 1; 2; 3 ]

The second line prints [1; 2; 3]. %A is the specifier to reach for whenever you want to see "what is in this thing".

Comments

FSHARP
// A single-line comment

(* A block comment
   spanning several lines *)

/// An XML documentation comment: shown in editor tooltips
let area width height = width * height

Triple slash /// comments attach to the next definition and show up when you hover over it in an editor. Use them on public functions.

Whitespace matters

F# has no curly braces around blocks. Indentation shows structure, using spaces only. The convention is four spaces per level:

FSHARP
let describeTemperature t =
    if t > 30 then
        printfn "Hot"
    else
        printfn "Comfortable"

Everything indented under let describeTemperature t = is the body. Lines that start at the same column belong together. Keep the rule in mind: a body must be indented further than the keyword that starts the definition, and sibling lines must line up exactly.

If a line is indented too little, the compiler either ends the block early or reports a warning such as "possible incorrect indentation". Never use tab characters; they are not allowed in F# source.

Modules and namespaces

You organise code into modules (containers for values and functions) and namespaces (containers for modules and types). Use open to bring names into scope:

FSHARP
namespace Geometry

module Circle =
    let pi = 3.14159
    let area radius = pi * radius * radius

Elsewhere you call Geometry.Circle.area 2.0, or open Geometry.Circle and just area 2.0. Standard library modules like List, String and Option are always available, and open System brings in .NET types such as Console and DateTime.

A simple file can also declare a top-level module in one line, and everything below belongs to it:

FSHARP
module Greetings

let hello name = $"Hello, {name}!"

$"..." is an interpolated string; you will study strings in Strings and formatting.

The entry point

An executable needs a starting point. The last file of the project can simply contain top-level code (as in the previous lesson). To be explicit, use [<EntryPoint>]:

FSHARP
module Program

open System

[<EntryPoint>]
let main argv =
    printfn "You passed %d argument(s)" argv.Length
    for a in argv do
        printfn " - %s" a
    0

The function takes the command-line arguments as a string array and returns an integer exit code: 0 means success. The attribute must be on the very last declaration of the last file. Many scripts and small apps skip all this and use top-level statements, which is perfectly fine.

Statements that return unit

printfn "hi" produces no useful value, so its type is unit, F#'s name for "nothing interesting" (written ()). You will see unit many times in Tuples and unit. For now know that a block of lines runs top to bottom, and each line that is not the last should be unit; otherwise the compiler warns that you are ignoring a result. Write |> ignore to discard on purpose.

Run the logic here: a C# stand-in

This C# program mirrors main above: it receives arguments and prints each. Because the runner passes none, we simulate a list.

C# C# stand-in: entry point with arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# C# stand-in: formatted output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does printfn "%s has %d items" "cart" 3 print?

    Arguments are separated by spaces, and each format specifier is matched with one argument.
  2. 2. How does F# show which lines belong to a function body?

    F# uses significant whitespace, with spaces rather than tabs.
  3. 3. What must [<EntryPoint>] main return?

    The return value is the process exit code; 0 means success.
  4. 4. Which specifier prints any value in a readable debugging format?

    %A pretty-prints lists, records, unions and more.

Summary

  • printfn, printf, sprintf and eprintfn format output with type-checked specifiers; %A prints anything.
  • Comments are //, (* *) and /// for documentation.
  • Indentation (spaces) defines blocks.
  • Modules and namespaces organise code; open brings names into scope.
  • [<EntryPoint>] marks main, which returns an int; top-level code is fine for small programs.

Next: Values and let bindings