# Math.Sqrt(x); not working?

**URL:** https://discourse.processing.org/t/math-sqrt-x-not-working/37681
**Category:** Beginners
**Tags:** homework
**Created:** [June 22, 2022, 12:01am UTC](https://discourse.processing.org/t/math-sqrt-x-not-working/37681 "2022-06-22T00:01:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![neon124](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neon124/32/16509_2.png) [@neon124](https://discourse.processing.org/u/neon124)
#### Post date: [June 22, 2022, 12:01am UTC](https://discourse.processing.org/t/math-sqrt-x-not-working/37681/1 "2022-06-22T00:01:58Z")

</div>

I’m making a calculator and when trying to make the square root function, it outputs the number you put in, not the square root. Here’s the bit of code that applies to the square root function.

```auto
SquareRoot = (Button)findViewById(R.id.SquareRoot);
        SquareRoot.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                x = TextBox.getText();
                xconv = Double.parseDouble(x.toString());
                Math.sqrt(xconv);
                answer = Double.toString(xconv);
                TextBox.setText(answer);
 
        }});

```

Just to give some information on this, x is a CharSequence, xconv is x converted to double, and answer is a string value. Thanks.

Edit: I have got the following resolution from [Scaler](https://www.scaler.com/topics/square-root-in-java/), but still unsure about it as I’m a beginner. Need someone’s insights into it.

```auto
 SquareRoot = (Button)findViewById(R.id.SquareRoot);
    SquareRoot.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
            x = TextBox.getText();
            xconv = Double.parseDouble(x.toString());
            xconv = Math.sqrt(xconv);//======> you are not initalize the answer to a variable here
            answer = Double.toString(xconv);
            TextBox.setText(answer);
 
    }});

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 22, 2022, 12:13am UTC](https://discourse.processing.org/t/math-sqrt-x-not-working/37681/2 "2022-06-22T00:13:47Z")

</div>

> [@neon124](#):
>
> it outputs the number you put in

That is what it appears to be coded to do…

Hint with Processing Java:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/b/9b6afb4301dbfc2e2c2ad52b319bcc67d041094f.png)

References:

- [https://processing.org/reference/assign.html](https://processing.org/reference/assign.html)
- [https://processing.org/reference/return.html](https://processing.org/reference/return.html)
- [https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Math.html](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Math.html)  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/4/a4d04f4390ccf6f6a4d51c331ced2fb45a93e2ed.png)

`:)`

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [June 22, 2022, 9:36am UTC](https://discourse.processing.org/t/math-sqrt-x-not-working/37681/3 "2022-06-22T09:36:57Z")

</div>

> [@neon124](#):
>
> `Math.sqrt(xconv);`

Java uses ‘pass by value’ parameters this means that the above code means

_ **calculate and return the square root of the ‘value’ of xconv** _

The value stored in the actual parameter `xconv` is never changed so the actual result has to be _assigned to a variable_ . Hence

```auto
xconv = Math.sqrt(xconv);

```

Which means  
_ **calculate and return the square root of the ‘value’ of xconv THEN assign the returned value to xconv** _
