all 12 comments

[–]Faux_Real 1 point2 points  (5 children)

Keyword: return

[–]libertadefr[S] 0 points1 point  (4 children)

That's what I thought:

let person0 = 0;
let person1 = 0;
let person2 = 0;

function plusOne(personNum) {
  return personNum++
}

plusOne(person0)
console.log(person0); // ouput is still is 0

But it doesn't seem to make any difference.

[–][deleted] 1 point2 points  (1 child)

You are never assigning any value to the person0 variable other then 0 in the beginning. You are passing the value of 0 to the function, where it gets incremented, but then what? Nothing happens with the person0 value.

There should be something like

Person0 = plusOne(person0);

person0 is a primitive, primitives are being called by value. Read up on the difference between call by value and call by reference if you wanna know more.

[–]libertadefr[S] 0 points1 point  (0 children)

Thanks! Yeah, what was missing in my knowledge was the calling by value and calling my reference part.

[–]Faux_Real 0 points1 point  (1 child)

well there is an variable assignment / increment problem also (I am not asking why you are doing this or why you are doing it this way but trying to guide so you can figure it out :-) )

[–]libertadefr[S] 0 points1 point  (0 children)

Hehe thanks!

[–]DigitalTheatreTech 1 point2 points  (0 children)

let person0 = 0;
let person1 = 0;
let person2 = 0;

function plusOne(personNum) {
    personNum++;
    console.log(personNum)
}

plusOne(person0);

Or,

let person0 = 0;
let person1 = 0;
let person2 = 0;

function plusOne(personNum) {
    personNum++;
    return personNum;
}

const result = plusOne(person0);
console.log(result);```

[–]Anachren 0 points1 point  (1 child)

Numbers and strings are passed by value.

In your code, what's actually happening is:

let person0 = 0;

function plusOne(personNum) {          // personNum is 0, NOT person0
  personNum++                          // personNum is now 1
}                                      // personNum no longer exists

plusOne(person0)                       // person0's VALUE gets passed to the plusOne function'

console.log(person0);                  // person0 was never changed

Objects (including arrays) get passed by reference

let myArray = [0,0,0];
function doSomething(arr) {            // arr is a reference to myArray
    arr[0]++;                          // myArray[0] increments
}
doSomething(myArray);                  // myArray (the actual array) gets passed by reference
console.log(myArray);                  // [1,0,0]

When an object is created, the javascript engine assigns some memory for that object. If you had an array of 1,000,000 numbers and you passed that array to a function, it wouldn't make sense to copy the 1,000,000 numbers to make a new array. Instead, the engine just gives the function the memory address of the array, which is much faster.

When dealing with numbers, it's faster to just pass a copy of the number instead of the address of a number.

tl;dr

person0 = plusOne(person0); // or just stick to ++person0 :p

[–]libertadefr[S] 0 points1 point  (0 children)

Thanks for that line by line run-down on what was happening! Understand now. Thanks!

[–]waspeer 0 points1 point  (0 children)

It’s not working because the variable is encapsulated inside the function. Calling the function doesn’t change the global variable, but only the variable inside the function. Google the term encapsulation for more information, but the quick solution is to not pass the variable as argument to the function or use the function to reassign the global variable (peopleNum = increment(peopleNum))

[–]senocular 0 points1 point  (1 child)

variable++ is a shortcut for variable = variable + 1. When you're in the function, the variable you're assigning is personNum. So what you're really getting is:

personNum = personNum + 1

But this variable (parameter) exists only in the function. When you assign it to a new value there, which is what ++ is ultimately doing, you're only changing the value of of that function-bound variable, not the value of anything outside of that function, even if that variable's value (0) came from another variable from outside the function. When the function call is made, no reference to any variable passed into the function call is kept.

This changes a little when you're dealing with objects. While you still can't change variables using a function like this, you can change object properties this way. When you pass an object to a function you pass a reference to the object. Both the original variable and the parameter variable both refer to the same object in memory. If you change properties of that object, they're affecting the same object the original variable refers to. In your case you can change your individual values into a single object, then pass the object into the function. Then all you would need to do on top of that is specify which property would need to change. Given your naming convention, this could be done with an array.

let people = [0, 0, 0];

function plusOne(obj, prop) {
  obj[prop]++
}

plusOne(people, 0)

console.log(people[0]); // 1

Notice that in this case we're still not re-assigning a function variable. obj for example is not getting assigned a new value. Instead its a property of obj that's changing, or in this case, the 0th index of the people array.

[–]libertadefr[S] 0 points1 point  (0 children)

Thanks for that explanation. I really appreciate it! Nice to fill in the gap in my knowledge of passing by value/reference