# Chapter 1 - Why both functions called in Ternary expression?

**URL:** <https://forums.kodeco.com/t/chapter-1-why-both-functions-called-in-ternary-expression/188661>\
**Category:** Expert Swift\
**Created:** [June 26, 2023, 9:25am UTC](https://forums.kodeco.com/t/chapter-1-why-both-functions-called-in-ternary-expression/188661 "2023-06-26T09:25:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tegieng](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tegieng/32/105896_2.png) [@tegieng](https://forums.kodeco.com/u/tegieng)\
**Post date:** [June 26, 2023, 9:25am UTC](https://forums.kodeco.com/t/chapter-1-why-both-functions-called-in-ternary-expression/188661/1 "2023-06-26T09:25:52Z")

</div>

Can anyone explain why both functions get called in Ternary expression here:

```auto
func ifelse(_ condition: Bool, _ valueTrue: Any, _ valueFalse: Any) -> Any {
   condition ? valueTrue : valueFalse
}

func expensiveValue1() -> Int {
    print("side-effect-1")
    return 2
}

func expensiveValue2() -> Int {
    print("side-effect-2")
    return 1729
}

let taxicab = ifelse(.random(), expensiveValue1(), expensiveValue2())

```

---

<div class="post-metadata">

**Author:** ![tegieng](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tegieng/32/105896_2.png) [@tegieng](https://forums.kodeco.com/u/tegieng)\
**Post date:** [June 26, 2023, 9:49am UTC](https://forums.kodeco.com/t/chapter-1-why-both-functions-called-in-ternary-expression/188661/2 "2023-06-26T09:49:53Z")

</div>

I figured out it, when passing them to ifelse function, they get called.

---

<div class="post-metadata">

**Author:** ![rayfix](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rayfix/32/108995_2.png) [@rayfix](https://forums.kodeco.com/u/rayfix)\
**Post date:** [June 27, 2023, 8:17am UTC](https://forums.kodeco.com/t/chapter-1-why-both-functions-called-in-ternary-expression/188661/3 "2023-06-27T08:17:31Z")

</div>

Right! And this is where @autoclosure comes to the rescue. 😃
