Allan  Macatingrao
Allan Macatingrao

Reputation: 2101

Swift: When To Use 'var' And 'func'

I always came across with this question when creating function or variable in swift. Consider the following implementations:

var

var isOpen: Bool
{
    // expression returning either true or false
}

var subTotal: Double
{
   return quantity * price
}

func

func isOpen() -> Bool
{
    // expression returning either true or false
}

func subTotal() -> Double
{
   return quantity * price
}

What's the best practice on this?

Upvotes: 1

Views: 386

Answers (1)

Wonjung Kim
Wonjung Kim

Reputation: 1933

I use var for fairy simple implementation with no side effect. If the computation takes long time or it changes something, I use func.

Upvotes: 2

Related Questions