Understanding Undefined value returned from a function ...sth still missing

Hello. Why is this challenge important…what do we use this knowledge for because it doesn’t seem useful to me in the future. Or perhaps it’s because I still don’t understand the purpose of this exercise…which is why I might need my function not to return anything but instead just change some global variable?
EXAMPLE 1:

// Setup
let sum = 0;
function addThree() {
  sum = sum + 3;
}
// Only change code below this line
function addFive() {
  sum = sum + 5;
}
// Only change code above this line
console.log(sum);
addThree();
console.log(addThree());
console.log(sum);
addFive();
console.log(addFive());
console.log(sum);

EXAMPLE 2:

// Setup
let sum = 0;
function addThree() {
  return sum = sum + 3;
}
// Only change code below this line
function addFive() {
  return sum = sum + 5;
}
// Only change code above this line
console.log(sum);
addThree();
console.log(addThree());
console.log(sum);
addFive();
console.log(addFive());
console.log(sum);

*So why would you need to avoid using a return statement when it makes much of a difference ‘to the function’ than not using it ,since when you do, it returns a ‘value’ than returning ‘undefined’…which Kinda sucks…please help me

Challenge: Understanding Undefined Value returned from a Function

**Link to the challenge:**https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/understanding-undefined-value-returned-from-a-function
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/understanding-undefined-value-returned-from-a-function

If you want to do something that is/has a side effect. So like

function pintHello () {
  console.log("Hello");
}

Or

function replaceWebPageWithHello () {
  document.body.text = "Hello";
}

Neither of those return anything, they just tell the computer to do something.

Function all work exactly the same. They take input and produce output. If you don’t specify what they return, then the return value is the value undefined. They can do other stuff though. Without side effects it would be extremely difficult to do anything useful

1 Like

Ok thanks a lot :clap: :clap: :clap: :clap: :clap:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.