Home

Records & Functions

   

A Record as Parameter

A record can be passed as argument to a function. To do this, include the argument in parentheses and specify the name of the record as data type. Here is an example:

let showApartment (apart : Apartment) = . . .

In the body of the function, you can access each member of the record by applying the period operator on the argument followed by the desired member of the record. Here is an example:

open System
open System.Drawing
open System.Windows.Forms

type Apartment = {
    UnitNumber      : string
    Bedrooms        : int
    Bathrooms       : float
    SecurityDeposit : int
    MonthlyRate     : int }

let apartmentRental : Form = new Form()
apartmentRental.Width <- 290
apartmentRental.Text <- "Lambda Square Apartments"
apartmentRental.Height <- 160

let lbxApartments = new ListBox()
lbxApartments.Left   <-  12
lbxApartments.Top    <-  12
lbxApartments.Width  <- 260
lbxApartments.Height <- 120
lbxApartments.Items.Add ("Unit #\tBeds\tBaths\tDeposit\tRate/month") |> ignore
lbxApartments.Items.Add ("------------------------------------------------------------------------------------") |> ignore
apartmentRental.Controls.Add lbxApartments

let showApartment(apart : Apartment) =
    lbxApartments.Items.Add (sprintf "%s\t%i\t%0.02f\t%i\t%i" apart.UnitNumber apart.Bedrooms apart.Bathrooms apart.SecurityDeposit apart.MonthlyRate) |> ignore

let a399475 = { UnitNumber = "101"; Bedrooms = 2; Bathrooms = 2.00; SecurityDeposit = 650; MonthlyRate = 1150 }
let a508293 = { UnitNumber = "102"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit = 500; MonthlyRate =  950 }
let a729397 = { a508293 with UnitNumber = "103" }
let a809387 = { UnitNumber = "104"; Bedrooms = 3; Bathrooms = 2.00; SecurityDeposit = 850; MonthlyRate = 1350 }
let a486360 = { a508293 with UnitNumber = "105"; MonthlyRate =  1050 }
let a273004 = { a399475 with UnitNumber = "106"; Bathrooms = 1.00; MonthlyRate = 1050 }

showApartment a399475
showApartment a508293
showApartment a729397
showApartment a809387
showApartment a486360
showApartment a273004

do Application.Run apartmentRental

Inside the function, besides accessing it, you can use or manipulate the record in any appropriate and allowed way.


     
 

Returning a Record Object

A function can be made to return a record object. At a minimum, you can have a function that creates its own record object and returns it.Here is an example:

type RoadSystem = {
    RoadName : string
    Distance : float
    Location  : string }

let create() = 
    let road = {
        RoadName = "U.S. Route 1"
        Distance = 2369.00
        Location = "From FortKent, Maine To Key West, Florida" }
    road // This is the returned record object

You can then call the function to retrieve the object it returns and use that object as you see fit. On the other hand, a function can be supplied some values as arguments and use all or some of those values to initialize a record and return it. Here is an example:

let createApartment nbr beds baths deposit rate : Apartment =
    let apart = { UnitNumber = nbr; Bedrooms = beds; Bathrooms = baths; SecurityDeposit = deposit; MonthlyRate = rate }
    apart // This is the returned record object

When calling the function, make sure you provide the appropriate values in the order the function expects them. You can get the value produced by that function and use its record object. Here is an example:

open System
open System.Drawing
open System.Windows.Forms

type Apartment = {
    UnitNumber      : string
    Bedrooms        : int
    Bathrooms       : float
    SecurityDeposit : int
    MonthlyRate     : int }

let apartmentRental : Form = new Form()
apartmentRental.Width <- 290
apartmentRental.Text <- "Lambda Square Apartments"
apartmentRental.Height <- 160

let lbxApartments = new ListBox()
lbxApartments.Left   <-  12
lbxApartments.Top    <-  12
lbxApartments.Width  <- 260
lbxApartments.Height <- 120
lbxApartments.Items.Add ("Unit #\tBeds\tBaths\tDeposit\tRate/month") |> ignore
lbxApartments.Items.Add ("------------------------------------------------------------------------------------") |> ignore
apartmentRental.Controls.Add lbxApartments

let showApartment(apart : Apartment) =
    lbxApartments.Items.Add (sprintf "%s\t%i\t%0.02f\t%i\t%i" apart.UnitNumber apart.Bedrooms apart.Bathrooms apart.SecurityDeposit apart.MonthlyRate) |> ignore

let createApartment nbr beds baths deposit rate : Apartment =
    let apart = { UnitNumber = nbr; Bedrooms = beds; Bathrooms = baths; SecurityDeposit = deposit; MonthlyRate = rate }
    apart

let a399475 = createApartment "101" 2 2.00 650 1150
let a508293 = createApartment "102" 1 1.00 500 950
let a729397 = { a508293 with UnitNumber = "103" }
let a809387 = createApartment "104" 3 2.00 850 1350
let a486360 = { a508293 with UnitNumber = "105"; MonthlyRate =  1050 }
let a273004 = { a399475 with UnitNumber = "106"; Bathrooms = 1.00; MonthlyRate = 1050 }

showApartment a399475
showApartment a508293
showApartment a729397
showApartment a809387
showApartment a486360
showApartment a273004

do Application.Run apartmentRental
   
   
 

Home Copyright © 2015, FunctionX Home