# How to use ternary operator for assigning a function

**URL:** <https://community.freefem.org/t/how-to-use-ternary-operator-for-assigning-a-function/896>\
**Category:** General Discussion\
**Created:** [April 6, 2021, 10:56pm UTC](https://community.freefem.org/t/how-to-use-ternary-operator-for-assigning-a-function/896 "2021-04-06T22:56:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arousta](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@arousta](https://community.freefem.org/u/arousta)\
**Post date:** [April 6, 2021, 10:56pm UTC](https://community.freefem.org/t/how-to-use-ternary-operator-for-assigning-a-function/896/1 "2021-04-06T22:56:22Z")

</div>

I want to do a function dispatch like below:

> func real a(real s) {return s\*s; }  
> func real c(real s) {return s-1; }  
> func myf = true ? a : c;

of course I want to use something other than true for the condition. But I get error for this. Any clean way to achieve this?

---

<div class="post-metadata">

**Author:** ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)\
**Post date:** [April 7, 2021, 5:00am UTC](https://community.freefem.org/t/how-to-use-ternary-operator-for-assigning-a-function/896/2 "2021-04-07T05:00:54Z")

</div>

functions `a` and `c` expect a real argument…  
you can also use a macros like

> macro myf1(condition,s )( (condition ? s \* s : s-1) ) //  
> macro myf2(s )( (true ? s \* s : s-1) ) //

---

<div class="post-metadata">

**Author:** ![arousta](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@arousta](https://community.freefem.org/u/arousta)\
**Post date:** [April 7, 2021, 4:47pm UTC](https://community.freefem.org/t/how-to-use-ternary-operator-for-assigning-a-function/896/3 "2021-04-07T16:47:54Z")

</div>

Thanks, one thing I forgot to mention is that I want to pass this myf as an argument to another function (e.g. like LinearCG which takes functions as input). Macros cannot handle this I guess.

---

<div class="post-metadata">

**Author:** ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)\
**Post date:** [April 8, 2021, 7:54am UTC](https://community.freefem.org/t/how-to-use-ternary-operator-for-assigning-a-function/896/4 "2021-04-08T07:54:40Z")

</div>

Right. Then for a function

> real K=1;  
> func myf = (true ? a(K) : c(K));

Note you have to be careful about the the type expected by the other function, and the type of the arguments as well… For example,  
if the function should be real, with real argument

> func real myf1(real aK) {return (true ? aa(aK) : cc(aK)); }

---

<div class="post-metadata">

**Author:** ![arousta](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@arousta](https://community.freefem.org/u/arousta)\
**Post date:** [April 8, 2021, 7:15pm UTC](https://community.freefem.org/t/how-to-use-ternary-operator-for-assigning-a-function/896/5 "2021-04-08T19:15:12Z")

</div>

Thanks, I think the second form is good enough and works fine 🙂
