# Understanding function evaluation ...

**URL:** <https://community.wolfram.com/t/understanding-function-evaluation/15287>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [January 23, 2020, 4:36pm UTC](https://community.wolfram.com/t/understanding-function-evaluation/15287 "2020-01-23T16:36:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [January 23, 2020, 4:36pm UTC](https://community.wolfram.com/t/understanding-function-evaluation/15287/1 "2020-01-23T16:36:26Z")

</div>

_(Apparently I had problems finding a proper title for this post!)_

Dear All,

I was playing around with the simple idea of switching evaluation of some functions globally off or on. Please have a look at the following minimal example:

```
ClearAll[func, validQ, foo]

(* defining some function - with "evaluation switch": *)
func[arg_?validQ] := arg

(* switching evaluation OFF: *)
validQ[_] = False;

(* making a simple setting: *)
foo = func[a];

(* result - as expected: *)
{foo, func[a]}
(* Out: {func[a],func[a]} *)

(* switching evaluation ON: *)
validQ[_] = True;

(* `foo` does not evaluate! *)
{foo, func[a]}
(* Out: {func[a], a} *)

```

So my question is: Why on Earth I cannot make `foo` evaluate. Obviously I am missing here some very basic point …

Regards and many thanks – Henrik

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [January 23, 2020, 4:58pm UTC](https://community.wolfram.com/t/understanding-function-evaluation/15287/2 "2020-01-23T16:58:23Z")

</div>

Interesting question for which I have no answer unfortunately, but I hope to find out by leaving a message here

---

<div class="post-metadata">

**Author:** ![neils](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/neils/32/20595_2.png) [@neils](https://community.wolfram.com/u/neils)\
**Post date:** [January 23, 2020, 4:59pm UTC](https://community.wolfram.com/t/understanding-function-evaluation/15287/3 "2020-01-23T16:59:28Z")

</div>

Henrik,

The problem is because of Set[] (=) vs SetDelayed[] (:=)

When you defined foo with =, validQ was false. If you define foo with := it will use the value of validQ at evaluation time, not at define time.

I hope this helps,

Neil

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [January 23, 2020, 5:01pm UTC](https://community.wolfram.com/t/understanding-function-evaluation/15287/4 "2020-01-23T17:01:20Z")

</div>

I tried that, changing all to SetDelayed, but also didn’t work for me

---

<div class="post-metadata">

**Author:** ![neils](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/neils/32/20595_2.png) [@neils](https://community.wolfram.com/u/neils)\
**Post date:** [January 23, 2020, 5:02pm UTC](https://community.wolfram.com/t/understanding-function-evaluation/15287/5 "2020-01-23T17:02:03Z")

</div>

Oops, I guess I should have tried it first!

---

<div class="post-metadata">

**Author:** ![hansmilton](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hansmilton/32/8656_2.png) [@hansmilton](https://community.wolfram.com/u/hansmilton)\
**Post date:** [January 23, 2020, 6:09pm UTC](https://community.wolfram.com/t/understanding-function-evaluation/15287/6 "2020-01-23T18:09:58Z")

</div>

In[1]:= func[arg\_ /; validQ] := arg  
foo[a\_] := func[a]

```
In[3]:= validQ = False;
        foo[p]
Out[4]= func[p]
    
In[5]:= validQ = True;
        foo[p]
Out[6]= p

```

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [January 23, 2020, 9:27pm UTC](https://community.wolfram.com/t/understanding-function-evaluation/15287/7 "2020-01-23T21:27:37Z")

</div>

Dear Sander, Neil, Hans,

many thanks for looking into that problem! Using `SetDelayed` was my first guess too. [@Hans Milton](https://community.wolfram.com/web/hansmilton): Yes, I had found that out already, thank you, and this is probably the way to go. But I basically would like to _understand_, why my approach above is not working …

Best regards – Henrik  
[at0]: [https://community.wolfram.com/web/hansmilton](https://community.wolfram.com/web/hansmilton)

---

<div class="post-metadata">

**Author:** ![kubapod](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kubapod/32/18662_2.png) [@kubapod](https://community.wolfram.com/u/kubapod)\
**Post date:** [January 24, 2020, 8:29am UTC](https://community.wolfram.com/t/understanding-function-evaluation/15287/8 "2020-01-24T08:29:06Z")

</div>

That is exactly the use case for `Update` although the documentation about it won’t give you in depth insights.

Shortly, once evaluated expressions are internally marked as such and won’t undergo reevaluation for efficiency purposes. Unless something triggers them, like `Update`.

```
validQ[_] = True;    
Update[func];

{foo, func[a]}

```

> ```
> {a, a}
> 
> ```

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [January 24, 2020, 1:58pm UTC](https://community.wolfram.com/t/understanding-function-evaluation/15287/9 "2020-01-24T13:58:22Z")

</div>

Hello Kuba, very interesting, perfect explanation, many thanks!

Best regards – Henrik
