Home

F# Topics: The Unit Type

   

Description

In some cases, you may not want a function to return a regular value, or a function may not return a known value, or the return value of a function is not clearly known. Such a function is said to return a void or a unit type. Actually, the void keyword is mostly used in C-based languages to represent the same thing.

The unit type is represented as an empty placeholder, represented as empty parentheses (). When you must use a unit type in a function, such as if passing an argument of unknown type, pass the type as unit. Here is an example:

open System
open System.Windows.Forms

let create(unit) = 
    let exercise : Form = new Form()
    exercise.Text <- "Graphical User Interface"

    Application.Run(exercise)

create()

Or this (no parentheses)

open System
open System.Windows.Forms

let create unit = 
    let exercise : Form = new Form()
    exercise.Text <- "Graphical User Interface"

    Application.Run(exercise)

create()

To indicate that a function doesn't return a value, specify its return type as unit. Here is an example:

open System
open System.Windows.Forms

let create() : unit = 
    let exercise : Form = new Form()
    exercise.Text <- "Graphical User Interface"

    Application.Run(exercise)

create()
 
   
     
 

Home Copyright © 2009-2015 FunctionX Home