Home

Inline Functions

   

Description

If you create a function that uses a long body, which means its implementation spans various lines, and if you call that function many times, everytime the function is called, the compiler must "travel" from the section where the function is called to where it is defined. This travel can be done too many times, putting a burden on the computer processor. On the other hand, if the function is short, to cut this travel, you can ask the compiler to bring the definition of the function to where it is called. To do this, when defining the function, precede it with the inline keyword. Here is an example:

open System
open System.Windows.Forms

let calculateDownPayment (loanAmount : float) (dpRate : float) =
    let rate = dpRate / 100.00
    loanAmount * rate

let inline calculateFinancingAmount (loanAmount : float) (downPayment : float) = loanAmount - downPayment

// Form: Loan Evaluation
let loanEvaluation = new Form(Width = 260, Height = 225, Text = "Loan Evaluation")

// Loan Amount
loanEvaluation.Controls.Add(new Label(Left = 21, Top = 20, Width = 80, Text = "Loan Amount:"))
let txtLoanAmount : TextBox = new TextBox(Left = 155, Top = 15, Width = 75, Text = "0.00")
loanEvaluation.Controls.Add txtLoanAmount

// Down Payment Rate
loanEvaluation.Controls.Add(new Label(Left = 21, Top = 46, Width = 130, Text = "Down Payment Rate:"))
let txtDownPaymentRate : TextBox = new TextBox(Left = 155, Top = 43, Width = 50, Text = "0.00")
loanEvaluation.Controls.Add txtDownPaymentRate
loanEvaluation.Controls.Add(new Label(Left = 214, Top = 47, Width = 20, Text = "%"))

// Down Payment Amount
loanEvaluation.Controls.Add(new Label(Left = 27, Top = 105, Width = 127, Text = "Down Payment Amount:"))
let txtDownPaymentAmount : TextBox = new TextBox(Left = 154, Top = 102, Width = 75, Text = "0.00")
loanEvaluation.Controls.Add txtDownPaymentAmount

// Amount to Finance
loanEvaluation.Controls.Add(new Label(Left = 27, Top = 130, Width = 102, Text = "Amount to Finance:"))
let txtFinancingAmount : TextBox = new TextBox(Left = 154, Top = 131, Width = 75, Text = "0.00")
loanEvaluation.Controls.Add txtFinancingAmount

// Button: Calculate
let btnCalculate = new Button(Left = 155, Top = 71, Text = "Calculate")

let btnCalculateClick e =
    let principal       = float txtLoanAmount.Text
    let downPaymentRate = float txtDownPaymentRate.Text
    
    let downPayment   = calculateDownPayment principal downPaymentRate
    let financeAmount = calculateFinancingAmount principal downPayment

    let strDownPayment   = sprintf "%0.02f" downPayment
    let strFinanceAmount = sprintf "%0.02f" financeAmount

    txtDownPaymentAmount.Text <- strDownPayment
    txtFinancingAmount.Text <- strFinanceAmount

btnCalculate.Click.Add btnCalculateClick
loanEvaluation.Controls.Add btnCalculate

// Button: Close
let btnClose = new Button(Left = 155, Top = 160, Text = "Close")
let btnCloseClick _ =  loanEvaluation.Close()
btnClose.Click.Add btnCloseClick
loanEvaluation.Controls.Add btnClose

do Application.Run loanEvaluation

Here is an example of executing the program:

Inline Functions

Inline Functions

     
     
 

Home Copyright © 2009-2015, FunctionX Home