# 'Nature of Code' Chapter 2 Query

**URL:** <https://discourse.processing.org/t/nature-of-code-chapter-2-query/28353>\
**Category:** Coding Questions\
**Created:** [March 8, 2021, 7:35pm UTC](https://discourse.processing.org/t/nature-of-code-chapter-2-query/28353 "2021-03-08T19:35:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scolty2021](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@scolty2021](https://discourse.processing.org/u/scolty2021)\
**Post date:** [March 8, 2021, 7:35pm UTC](https://discourse.processing.org/t/nature-of-code-chapter-2-query/28353/1 "2021-03-08T19:35:29Z")

</div>

Hi,

Thanks for taking the trouble to click on this Topic.

I’m currently working my way thru Daniel Shiffman’s Learning Processing & the Nature of Code books, and have the following query about a section in the latter, chapter 2 (Forces), section 2.3, p.69.

He uses the following code for simulating the effects of wind on a bouncing ball (the wind is only in-effect when the mouse button is pressed):

if (mousePressed) {  
PVector wind = new PVector(0.5,0);  
mover.applyForce(wind);  
}

My interpretation of his code (ignoring the line mover.applyFoce(wind;) is as follows: when the mouse is pressed, a PVector object named _wind_ is created. If / when the mouse is pressed again, Processing then creates another PVector object, also named _wind_?

I would expect the code above to result in an error, because an object already called _wind_ exists. However, there is no error, and the code runs fine. Could someone explain why this is the case, as I’m a bit confused. Does the instances of _wind_ only last the length of the if statement? Clearly I’ve misinterpreted.

Any help etc. will be much appreciated.

Kind regards,

Scolty

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [March 8, 2021, 7:43pm UTC](https://discourse.processing.org/t/nature-of-code-chapter-2-query/28353/2 "2021-03-08T19:43:57Z")

</div>

Almost. When you create a variable inside a function, it is a local variable that only exists within that function, and only until that function is done. Trying to access that variable from a different function would be an error.

Variables that are created outside of functions are global variables, and can be used inside any function.

You can learn more about this by searching “variable scope”. I’m sure you’ll find numerous guides online that will explain it better than I can.

---

<div class="post-metadata">

**Author:** ![scolty2021](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@scolty2021](https://discourse.processing.org/u/scolty2021)\
**Post date:** [March 8, 2021, 8:53pm UTC](https://discourse.processing.org/t/nature-of-code-chapter-2-query/28353/3 "2021-03-08T20:53:51Z")

</div>

Thanks you for replying TfGuy44, it is really appreciated. Embarrassingly, I have been jumping between chapters in Learning Processing and missed the section on Local vs Global variables. I guess where I’m still getting confused is that the _wind_ in the code above is an object, variables I think of as type boolean, int, float etc. But I tried calling _wind_ elsewhere in the class, and as you advised, it didn’t work.

Thanks again for your help; I hadn’t anticipated such a prompt & constructive reply :- )

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 8, 2021, 9:33pm UTC](https://discourse.processing.org/t/nature-of-code-chapter-2-query/28353/4 "2021-03-08T21:33:46Z")

</div>

Hello, @scolty2021, and welcome to the Processing Forum!

A variable can refer to any type of object, so long as it is declared and then assigned correctly. In your code, `wind` refers to a `PVector` object that is being used to represent a force applied by wind.

This statement declares `wind` as a local variable that can reference a `PVector` object, then instantiates a `PVector` object, and finally assigns it to `wind`:

```auto
PVector wind = new PVector(0.5,0);

```

The following statement then calls an `applyForce` method from an object represented by a variable, `mover`, passing `wind` as an argument. Perhaps `mover` represents the ball, and this statement applies the force of the wind to the ball:

```auto
mover.applyForce(wind);

```

_EDIT (March 8, 2021):_

> [@scolty2021](#):
>
> Does the instances of _wind_ only last the length of the if statement?

It only exists for the remainder of the block in which it was declared.

Following is an example from [w3schools.com:Java Scope: Block Scope](https://www.w3schools.com/java/java_scope.asp):

```auto
public class Main {
  public static void main(String[] args) {

    // Code here CANNOT use x

    { // This is a block

      // Code here CANNOT use x

      int x = 100;

      // Code here CAN use x
      System.out.println(x);

   } // The block ends here

  // Code here CANNOT use x

  }
}

```

As given, it would run without an error.

However, if you modify it as follows, it would raise an error:

```auto
public class Main {
  public static void main(String[] args) {

    // Code here CANNOT use x

    { // This is a block

      // Code here CANNOT use x

      int x = 100;

      // Code here CAN use x
      System.out.println(x);

   } // The block ends here

  // Code here CANNOT use x
  System.out.println(x); // This raises an error!
  
  }

```

Output:

```
Main.java:18: error: cannot find symbol
  System.out.println(x); // This raises an error!
                     ^
  symbol: variable x
  location: class Main
1 error

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 9, 2021, 12:16pm UTC](https://discourse.processing.org/t/nature-of-code-chapter-2-query/28353/5 "2021-03-09T12:16:37Z")

</div>

> [@scolty2021](#):
>
> If / when the mouse is pressed again, Processing then creates another PVector object, also named _wind_ ?

It’s not _another_ PVector object, the old one gets overwritten

> [@scolty2021](#):
>
> I would expect the code above to result in an error, because an object already called _wind_ exists.

I think it ceased to exist.

> [@scolty2021](#):
>
> Does the instances of _wind_ only last the length of the if statement?

correct

---

<div class="post-metadata">

**Author:** ![scolty2021](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@scolty2021](https://discourse.processing.org/u/scolty2021)\
**Post date:** [March 9, 2021, 9:52pm UTC](https://discourse.processing.org/t/nature-of-code-chapter-2-query/28353/6 "2021-03-09T21:52:27Z")

</div>

Hi javagar, I appreciate you replying to my post & for the welcome. Thank you also for clarify my mix-up regarding variables & objects - I’m very much a beginner programmer, so please forgive the very basic question.

Regarding your update, the _wind_ PVector only lasts until the next closing bracket ‘}’, and because the ‘_x_’ in ‘_System. **out**.println(x)_’ in your last example is outside the block in which x is declared, an error occurs, that makes more sense.

Thanks again for your patience and taking the trouble to reply, it is much appreciated.

Kind regards,

Scolty.

---

<div class="post-metadata">

**Author:** ![scolty2021](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@scolty2021](https://discourse.processing.org/u/scolty2021)\
**Post date:** [March 9, 2021, 9:58pm UTC](https://discourse.processing.org/t/nature-of-code-chapter-2-query/28353/7 "2021-03-09T21:58:23Z")

</div>

Hi Chrisir, thanks for also replying to my post - I wish I had joined the Processing forum sooner, having now realised how helpful and knowledgeable it’s members, like yourself, are.

Kind regards,

Scolty
