# Composition of functions

**URL:** <https://community.freefem.org/t/composition-of-functions/603>\
**Category:** General Discussion\
**Created:** [October 6, 2020, 6:27pm UTC](https://community.freefem.org/t/composition-of-functions/603 "2020-10-06T18:27:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mordechaiy](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mordechaiy](https://community.freefem.org/u/mordechaiy)\
**Post date:** [October 6, 2020, 6:27pm UTC](https://community.freefem.org/t/composition-of-functions/603/1 "2020-10-06T18:27:59Z")

</div>

Hi everyone,

I would like to ask please,

I defined the following functions :

h1(z)=(abs(z)-1)^2\*(1+2\*abs(z)), for -1\<z\<1 otherwise h1(z)=0.

z(x,y)=((x-x0)\*cos(theta)+(y-y0)_sin(theta)+c_t)/lambda.

and would like to compose h1(z(x,y))

I created two functions in FF++ but when I composed them, FF++ doesn’t compute the composed function.

real theta=pi/4;  
real c=1;  
real lambda=1;  
real xr=3,yr=3;  
real x0=xr+lambda_cos(theta),y0=yr+lambda_sin(theta);

func h1= ((abs(z)-1)^2\*(1+2_abs(z)))_(z\>-1 && z\<1);  
func z=((x-x0)\*cos(theta)+(y-y0)_sin(theta)+c_t)/lambda;  
func utilde=h1(z(x,y));// compse h1(z)

but FF++ tells me that there is an error in the definition of utilde.

Thank you!

Mordechai.

---

<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:** [October 6, 2020, 7:38pm UTC](https://community.freefem.org/t/composition-of-functions/603/2 "2020-10-06T19:38:31Z")

</div>

Hi. naively there are most likely missing types and arguments , you define `func z` with no arguments… maybe something like

> func real h1(real zz)= ((abs(zz)-1)^2\*(1+2_abs(zz)))_(zz\>-1 && zz\<1);  
> func real z0(real xx, real yy)=((xx-x0)\*cos(theta)+(yy-y0)_sin(theta)+c_t)/lambda;  
> func real utilde=h1(z0(x,y));// compse h1(z)

Note that maybe z is a reserved word. Also you could simply use macros like

> macro z(xx,yy)=((xx-x0)\*cos(theta)+(yy-y0)_sin(theta)+c_t)/lambda //EOM

---

<div class="post-metadata">

**Author:** ![mordechaiy](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mordechaiy](https://community.freefem.org/u/mordechaiy)\
**Post date:** [October 6, 2020, 7:43pm UTC](https://community.freefem.org/t/composition-of-functions/603/3 "2020-10-06T19:43:03Z")

</div>

Thank you so much Julien, for your reply! I appreciate it.  
.

---

<div class="post-metadata">

**Author:** ![mordechaiy](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mordechaiy](https://community.freefem.org/u/mordechaiy)\
**Post date:** [October 6, 2020, 9:17pm UTC](https://community.freefem.org/t/composition-of-functions/603/4 "2020-10-06T21:17:56Z")

</div>

Hi,  
I tried to use the macro command.

the code runs but, it doesn’t give the right result.

the right result should be that from the right, top and left edges of the square there should be a wave propagating in one direction.

Could you please check where is the problem?

Thank you,

Mordechai.  
[u incident.edp](https://community.freefem.org/uploads/short-url/oYrG8Rjsz0BCVwYil0cRElDkVXQ.edp) (1.4 KB)
