# Return Statement Wont Print

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 ?

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.

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

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

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);
``````

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.

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?

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

``````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.

``````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.

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

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

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 ?

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

Try using fill() instead of stroke()

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