Home > Blockchain >  When I call a function, should I assign the values that are returned in the functions to the variabl
When I call a function, should I assign the values that are returned in the functions to the variabl

Time:08-12

Let's say we have three variables, a, b and c;

The variable 'a' is assigned a boolean value of "false",
the variable 'b' is assigned an integer number of 10 and
the variable 'c' contains a list of [1, 2, 3, 4, 5] as an array.

After that, we have a function that modifies all of the values of the above three variables as follows:

#1) This is the Function that modifies the values declared outside the function.

function modifyAllValues(a, b, c){
    a = true;
    b = b   10;
    c.pop();

    return [a, b, c];
}

#2) This is the MAIN Part of the Program where the variables are declared, and where the function will be called:

$(document).ready(function(){
    let a = false;
    let b = 10;
    let c = [1, 2, 3, 4, 5];
});

My Question here is which one of the following is the right way to get the updated/modified values that are returned by modifyAllValues function, A or B?

A)

$(document).ready(function(){
    /*-----method 1-----*/

    let tempArray = modifyAllValues(a, b, c);
    /* Assigning the returned values back to the declared variables */
    a = tempArray[0];
    b = tempArray[1];
    c = tempArray[2];

    //console.log(a) => true
    //console.log(b) => 20
    //console.log(c) => [1, 2, 3, 4]
});

B)

$(document).ready(function(){
    /*-----method 2-----*/

    let tempArray = modifyAllValues(a, b, c);
    /* Using the values without assigning the returned values to variables */
    //console.log(a) => true
    //console.log(b) => 20
    //console.log(c) => [1, 2, 3, 4]
});

Thank You So Much!

CodePudding user response:

Consider the following example.

$(function() {
  // Declaration of Variables
  var a = false;
  var b = 10;
  var c = [1, 2, 3, 4, 5];

  // Declaration of Function
  function modifyAllValues() {
    a = true;
    b = b   10;
    c.pop();
  }

  // Event
  modifyAllValues();

  // Output
  console.log(a, b, c);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

As the variables were defined, outside the function, in a more global space, they can be modified by a function.

Now consider the following.

$(function() {
  // Declaration of Variables
  var x = false;
  var y = 10;
  var z = [1, 2, 3, 4, 5];

  // Declaration of Function
  function modifyAllValues(a, b, c) {
    a = true;
    b = b   10;
    c.pop();

    return {
      "a": a,
      "b": b,
      "c": c
    };
  }

  // Event
  var results = modifyAllValues(x, y, z);
  x = results.a;
  y = results.b;
  z = results.c;

  // Output
  console.log(x, y, z);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

This accepts parameters, modifies the, and returns them. a, b, and c are only defined within the scope of the function.

  • Related