# Summing func variables

**URL:** https://community.freefem.org/t/summing-func-variables/2147
**Category:** General Discussion
**Created:** [November 29, 2022, 12:18am UTC](https://community.freefem.org/t/summing-func-variables/2147 "2022-11-29T00:18:36Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jsmacd](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jsmacd](https://community.freefem.org/u/jsmacd)
#### Post date: [November 29, 2022, 12:18am UTC](https://community.freefem.org/t/summing-func-variables/2147/1 "2022-11-29T00:18:36Z")

</div>

Is it possible to define a function in a loop.

For example, I want something like:

func loopFunc = 0;  
for(int i = 2; i \< m; i++){  
a = i_l-w;  
b = i_l+w;  
cout \<\< "a = " \<\< a \<\< ", b = " \<\< b \<\< endl;  
loopFunc += (abs(x - 0.5) \< myEps && abs(y - 0.5) \< myEps && z\>a && z\<b);  
}

When I try this, it doesn’t want to perform the operation.

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [December 8, 2022, 4:02pm UTC](https://community.freefem.org/t/summing-func-variables/2147/2 "2022-12-08T16:02:30Z")

</div>

I am not sure what your question is exactly, but this is an incorrect use of a `func`. It should not appear on the left hand side in the second to last line.  
[https://doc.freefem.org/references/types.html#functions-design](https://doc.freefem.org/references/types.html#functions-design)

---

<div class="post-metadata">

### Author: ![jsmacd](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jsmacd](https://community.freefem.org/u/jsmacd)
#### Post date: [December 8, 2022, 5:39pm UTC](https://community.freefem.org/t/summing-func-variables/2147/3 "2022-12-08T17:39:21Z")

</div>

Thank you, Chris. What I would like to build is essentially a step function that I can easily change how many steps it has with one parameter. For example, how can I build something like:

f = 0, x \< 0 and x \> J  
i, i - 1\< x \< i, i = 1, …, J.

I’d like to build it in a loop where I can easily change how many steps there are with a simple change of the parameter J. Especially since sometimes I want many steps.

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [December 8, 2022, 6:31pm UTC](https://community.freefem.org/t/summing-func-variables/2147/4 "2022-12-08T18:31:57Z")

</div>

It is not clear what you want based on that pseudocode. Have you read the documentation? Have you looked at the examples?

---

<div class="post-metadata">

### Author: ![jsmacd](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jsmacd](https://community.freefem.org/u/jsmacd)
#### Post date: [December 8, 2022, 8:57pm UTC](https://community.freefem.org/t/summing-func-variables/2147/5 "2022-12-08T20:57:22Z")

</div>

Yes, I have, and I’ve tried many things. I can’t seem to get it to run properly though. For example, I can get a running code doing the following, but f doesn’t plot like g.

```auto
int J = 6;
func f = 0;
for(int i = 0; i < J; i+=2){
  cout << "i = " << i << endl;
  func h = f + (x > i && x < i+1 && y > i && y < i+1);
  func f = h;
}

func g = (x > 0 && x < 1 && y > 0 && y < 1) + (x > 2 && x < 3 && y > 2 && y < 3) + (x > 4 && x < 5 && y > 4 && y < 5);

border B1(t = 0, 10){x = t; y = 0; label = 1;};
border B2(t = 0, 10){x = 10; y = t; label = 2;};
border B3(t = 0, 10){x = 10-t; y = 10; label = 3;};
border B4(t = 0, 10){x = 0; y = 10-t; label = 4;};

mesh Th = buildmesh(B1(50) + B2(50) + B3(50) + B4(50));

fespace Vh(Th, P1);
Vh gproj = g, fproj = f;

plot(fproj, wait = true, cmm = "fproj");
plot(gproj, Th, wait = true, cmm = "gproj");

```

I want to be able to build the function in the loop because I want to be able to make J as large or small as I want without having to manually add or subtract terms.

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [December 10, 2022, 8:44am UTC](https://community.freefem.org/t/summing-func-variables/2147/6 "2022-12-10T08:44:21Z")

</div>

Thank you, I think I see your problem now. Here’s a version of your example that works. Take a look and let me know if you have any questions.

```auto
int J = 6;
/* This section of code does not make sense
func f = 0;
for(int i = 0; i < J; i+=2){
  cout << "i = " << i << endl;
  func h = f + (x > i && x < i+1 && y > i && y < i+1);
  func f = h;
}
*/
// Maybe this is what you're looking for?
func real f(int J){
  real h;
  for (int i = 0; i < J; i+=2){
    //cout << "i = " << i << endl; This will print a lot if kept
    h += (x > i && x < i+1 && y > i && y < i+1);
  }
  return h;
}

func g = (x > 0 && x < 1 && y > 0 && y < 1) + (x > 2 && x < 3 && y > 2 && y < 3) + (x > 4 && x < 5 && y > 4 && y < 5);

//border B1(t = 0, 10){x = t; y = 0; label = 1;};
//border B2(t = 0, 10){x = 10; y = t; label = 2;};
//border B3(t = 0, 10){x = 10-t; y = 10; label = 3;};
//border B4(t = 0, 10){x = 0; y = 10-t; label = 4;};
//mesh Th = buildmesh(B1(50) + B2(50) + B3(50) + B4(50));

// Here's an easier option to mesh the 10x10 square:
mesh Th = square(50,50,[10*x,10*y]);

fespace Vh(Th, P1);
Vh gproj = g, fproj = f(J);
//plot(fproj, wait = true, cmm = "fproj", fill = 1);
//plot(gproj, Th, wait = true, cmm = "gproj", fill = 1);
// Let's compare the difference instead
Vh diff = fproj-gproj;
plot(diff, wait = true, cmm = "fproj-gproj", fill = 1);

```

---

<div class="post-metadata">

### Author: ![jsmacd](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jsmacd](https://community.freefem.org/u/jsmacd)
#### Post date: [December 12, 2022, 10:57pm UTC](https://community.freefem.org/t/summing-func-variables/2147/7 "2022-12-12T22:57:54Z")

</div>

This is exactly what I wanted to do. Thank you very much, Chris! Clears up a lot of things in general.
