-2

I'm having trouble of understanding the very basic concept of returning a value in functions (JS etc).

Why should I use

function add(x,y){
  result=x+y;
  return result;
}
add(5,3);

instead of

var result=0;
function add(x,y){
  result=x+y;
}
add(5,3);

I guess it's about saving memory, isn't it?

Revo
  • 150
  • 2
  • 10
  • 2
    saving memory? not really, both of your functions require a global `result` var ... the standard way of writing that would me more like `function add(x, y) { return x + y; }` and the use would be `var result = add(2,3)` – Jaromanda X Jun 28 '17 at 06:27
  • Just to be clear, your first example won't work as-is. When a function returns something, just calling it like that doesn't make sense. –  Jun 28 '17 at 06:44

5 Answers5

0

Basically, when You use the 'return' keyword, the fuction Will assume the value that you return. Insted, when you use the normal operation, you just do an operation, but the value(x+y) will be not saved anywhere.

Hope you understand :)

0

It's not about memory, it's about how you want your code to work.

A function is mainly designed to simplify repeated tasks (Although it can be used only once).

Let's take your first function:

function add(x,y){
  result=x+y;
  return result;
}
var eight = add(5,3);
var nine = add(5,4);

How would we accomplish that with our second function?

var result=0;
function add(x,y){
  result=x+y;
}
add(5,3);
var eight = result;
add(5,4);
var nine = result;

This seems like a round about way of doing the same thing, instead of just simply returning it. While you can assign it to a global variable, for a simple function that returns 1 thing, you should simply use return. You would likely want to set outside variables only if it's going to effect other functions or the state of your application.

var locale = 'en';
var currency = '$';

function setConfig(locale, currency) {
    //Here we could use the currency string in other functions, so setting state would be nice here.
    locale = 'en';
    currency = '$';
}

function displayCurrency(num) {
    console.log(currency + num);
}
Blue
  • 22,608
  • 7
  • 62
  • 92
0

I don't think memory is a concern when deciding if you should use one of the two functions.

The main takeaway between those two functions is that as a developer, it's not good practice to have or use global variables, which in this case is var result = 0; It's also good practice to utilize variables within a scope, and Javascript is lexically scoped without the introduction of ES6's (ECMAScript2015) let and const keywords. Generally, functions are expected to return values instead of introducing side effects that may create unexpected behaviors (although it's hard to see in this basic function), which in this case, would be manipulating the global var result = 0

If anything is unclear, feel free to start a discussion with comments :)

0

Because first one is Reusable one.Its useful to connect the other function

function add(x,y){
  result=x+y;
  return result;
}
console.log(add(5,3));
console.log(add(5,3)+add(8,3));// easy to connect other model

And second one

  1. you assign the result in variable .Is not reusable.
  2. if you assign something in the variable after function call.The result will affected
    var result=0;
    function add(x,y){
      result=x+y;
    }
    add(5,3);
    result="affected";
    console.log(result)
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

First, functions and variables in Javascript are linked to a scope. In your second example your variable result is linked to the document scope. Here is works, but consider this code:

function add(x,y){
  result = x+y;
}
function alertAdd(x,y){
  var result = add(x,y);
  alert(result);
}
alertAdd(5,3);

If you run this, you'll get an alert with a value of 'Undefined' because the variable result is defined in alertAdd's scope. Meaning the main scope doesn't know about it and result in your add() function is not even defined in the first place. In other languages this would create an error.

Now consider this:

function add(x,y){
  return x+y;
}
function alertAdd(x,y){
  var result = add(x,y);
  alert(result);
}
alertAdd(5,3);

This works because event if add() and result are not declared on the same scope, we ask add() to return its value, not to assign it to a preexisting variable. Furthermore you could reuse add() on any variable. Not only result.

var result_b = add(1+2); // result_b = 3
var result_c = add(2+2); // result_c = 4
function logAdd(a,b) {
  var value = add(a,b);
  console.log(value);
}
logAdd(3,3); // 6
Viandoks
  • 156
  • 3