1
func runningMetersWithMetersPerDay ( metersPerDay: Int ) -> () -> 
Int {
    var totalMeters = 0
    return {
        totalMeters += metersPerDay
        return totalMeters
    }
}
var planA = runningMetersWithMetersPerDay (metersPerDay: 2000)
planA()
planA()

The first planA() print 2000 and the second planA()print 4000. The number accumulates each time I call planA() Why? What is the use of var totalMeters = 0 in this function ? Is planA() the same as runningMetersWith MetersPerDay (metersPerDay: 2000) ? If not, what's the difference between them ? Thanks for your help.

2 Answers 2

1

Well, your method returns a closure, which you excute twice. The closure captures it's environment, in your case the 'outer' variable totalMeters and keeps it's value through different calls.

If this is not what you want, you'd have to put totalMeters into the closure like this:

func runningMetersWithMetersPerDay ( metersPerDay: Int ) -> () ->
Int {
    return {
        var totalMeters = 0
        totalMeters += metersPerDay
        return totalMeters
    }
}

To answer the question from the comment below: I've modified the original function and calls, to print out some hints:

func runningMetersWithMetersPerDay ( metersPerDay: Int ) -> () ->
    Int {
        print ("rMWMD called")
        var totalMeters = 0
        return {
            totalMeters += metersPerDay
            print ("calculated new result: \(totalMeters)")
            return totalMeters
        }
}

print ("start")
var planA = runningMetersWithMetersPerDay (metersPerDay: 2000) // "rMWMPD called"
print ("got planA")
planA() // "calculated new result:2000"
planA() // "calculated new result:4000"

runningMetersWithMetersPerDay (metersPerDay: 2000)() // "calculated new result: 2000"
planA() // "calculated new result:6000"
print ("end")

planA is the result of the function runningMetersWithMetersPerDay (a closure of type (())->Int - take void paramters and return Int) , and you then execute the closure twice and get the results (2000, 4000).

Now, the function runningMetersWithMetersPerDay(metersPerDay:) is called again, returns a new closure and this return value is then excecuted. It returns 2000, since it's a new closure (although not assigned to a variable). Think of it as the following:

    runningMetersWithMetersPerDay (metersPerDay: 2000) /* "rMWMPD called" and now call the closure: */ () // "calculated new result: 2000"

At the end you execute planA a third time, and since it still holds it's own state (e.g. own totalMeters value), it returns 6000.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for your answer. But I find another small question. First I called planA() twice and the result prints 2000 and 4000, then I tried to call runningMetersWithMetersPerDay (metersPerDay: 2000)(), and the result prints 2000, then I called planA() again, the result prints 6000. Now that planA= runningMetersWithMetersPerDay (metersPerDay: 2000), why is it so different when I called planA() and runningMetersWithMetersPerDay (metersPerDay: 2000)() ?
@Wilson: Please look at my modified answer, hope it get's clear now.
0

What's happening here is normal. Let's go step by step.

First, you run:

var planA = runningMetersWithMetersPerDay (metersPerDay: 2000)

What do you execute here?

var totalMeters = 0
return {
    totalMeters += metersPerDay
    return totalMeters
}

What do you have in planA ?

  1. A context with a variable totalMeters
  2. A fonction ready to be executed within this context

First run, totalMeters = 0

{
    totalMeters += metersPerDay
    return totalMeters
}()

Second run, totalMeters = 2000

{
    totalMeters += metersPerDay
    return totalMeters
}()

new result = 4000

1 Comment

Kinda answered at the same time as Andreas :) haha! You're welcome Wilson.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.