# Return Statement Wont Print

**URL:** <https://forum.kirupa.com/t/return-statement-wont-print/643669>\
**Category:** Uncategorized\
**Created:** [July 26, 2020, 4:00pm UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669 "2020-07-26T16:00:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Stern](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/john_stern/32/12050_2.png) [@John\_Stern](https://forum.kirupa.com/u/John_Stern)\
**Post date:** [July 26, 2020, 4:00pm UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669/1 "2020-07-26T16:00:28Z")

</div>

var addNumbers = function(a,b) {  
var c = a + b;  
return c;  
};  
addNumbers(4,5);  
text(“addNumbers”,100,100);

why doesn’t this code print out the value of 9 ?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 26, 2020, 4:17pm UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669/2 "2020-07-26T16:17:55Z")

</div>

You’re ignoring the return so nothing happens with it. The return is what value replaces the function when it’s called. To capture it, you would usually assign it to a variable and then do something with the variable.

```auto
var addNumbers = function(a,b) {
    var c = a + b;
    return c;
};
var added = addNumbers(4,5);
text("addNumbers:" + added,100,100);

```

---

<div class="post-metadata">

**Author:** ![John\_Stern](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/john_stern/32/12050_2.png) [@John\_Stern](https://forum.kirupa.com/u/John_Stern)\
**Post date:** [July 26, 2020, 5:30pm UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669/3 "2020-07-26T17:30:38Z")

</div>

Great analysis of my problem - I get it now. Just wondering how I would alter / add to the code so that whatever variables are entered for a , b, the text would out put - a + b = c

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 26, 2020, 11:25pm UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669/4 "2020-07-26T23:25:29Z")

</div>

If I understood your question correctly, you can add your code to the addNumbers function itself:

```
var addNumbers = function(a,b) {
    var c = a + b;
    text("added numbers:" + c, 100, 100);
    return c;
};
var added = addNumbers(4,5);
text("addNumbers:" + added,100,100);

```

🙂

---

<div class="post-metadata">

**Author:** ![John\_Stern](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/john_stern/32/12050_2.png) [@John\_Stern](https://forum.kirupa.com/u/John_Stern)\
**Post date:** [July 27, 2020, 2:57am UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669/5 "2020-07-27T02:57:50Z")

</div>

**Sorry if my intent wasn’t clear - I simply wanted a printout of the equation for all values that we happened to enter for (a,b) so that the printout would look like**

2 + 7 = 9

1 + 19 = 20

13 + 9 = 22 etc.,

So I am uncertain as to how to interpret the code you have written since to me it appears that the top text line of code for a = 4 and b = 5

text(“added numbers:” + c, 100, 100);

would generate a readout - added numbers: 9

and the bottom text line of code

text(“addNumbers:” + added,100,100);

would generate a readout - addNumbers: 9

whereas I am simply looking for a readout - 4 + 5 = 9

So I’m not sure why we need 2 lines of code for text when both are assigned to the same location but in any case, neither line of code for text seems to give me the simple equation that I am looking for.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 27, 2020, 4:09pm UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669/6 "2020-07-27T16:09:17Z")

</div>

I did misunderstand what you were saying. Your follow-up explanation helped. What does your text function do? Does it show both your current as well as previous entries when you call it?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 27, 2020, 4:19pm UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669/7 "2020-07-27T16:19:04Z")

</div>

You would need to include the numbers yourself in your text call.

```javascript
var added = addNumbers(4,5);
text("4 + 5 = " + added,100,100);

```

You can wrap this up in a single function, though you start losing control over things like where to place the text.

```javascript
var addAndDisplayNumbers = function(a,b) {
    var c = a + b;
    text(a + " + " + b + " = " + c,100,100);
    return c;
};
addAndDisplayNumbers(4,5);

```

but you can also throw the positioning in as parameters too.

```javascript
var addAndDisplayNumbers = function(a,b,x,y) {
    var c = a + b;
    text(a + " + " + b + " = " + c,x,y);
    return c;
};
addAndDisplayNumbers(4,5,100,100);

```

---

<div class="post-metadata">

**Author:** ![John\_Stern](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/john_stern/32/12050_2.png) [@John\_Stern](https://forum.kirupa.com/u/John_Stern)\
**Post date:** [July 27, 2020, 6:57pm UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669/8 "2020-07-27T18:57:50Z")

</div>

Ahh - that last function that takes care of everything is so elegant - very impressive - thank you. I pasted the code into the program on Khan academy but nothing showed up whereas I would have expected to s

 ![Screen Shot 2020-07-27 at 11.53.13 AM](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/2X/1/16a64c9709acc67336e99f81d64b61bba8f5e834.png) ee 4 + 5 = 9 in the RS window. At first I thought it might be due to the lack of a color code above the text code but even when I placed a stroke code above the text code - still no output in the RS window - any idea why ?

---

<div class="post-metadata">

**Author:** ![John\_Stern](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/john_stern/32/12050_2.png) [@John\_Stern](https://forum.kirupa.com/u/John_Stern)\
**Post date:** [July 27, 2020, 7:50pm UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669/9 "2020-07-27T19:50:44Z")

</div>

The text function is only supposed to output the actual arithmetic operation that is being done at the moment

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 27, 2020, 7:59pm UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669/10 "2020-07-27T19:59:23Z")

</div>

Try using fill() instead of stroke()

---

<div class="post-metadata">

**Author:** ![John\_Stern](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/john_stern/32/12050_2.png) [@John\_Stern](https://forum.kirupa.com/u/John_Stern)\
**Post date:** [July 27, 2020, 10:32pm UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669/11 "2020-07-27T22:32:22Z")

</div>

ha ! yes, fill did the trick - I really thought that it was stroke not fill that brings color to text but I’m obviously wrong - good tip

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 27, 2020, 10:34pm UTC](https://forum.kirupa.com/t/return-statement-wont-print/643669/12 "2020-07-27T22:34:10Z")

</div>

👍
