let sum = 0;
function addSum(num) {
sum = sum + num;
}
addSum(3);
addSum is a function without a return statement. The function will change the global sum variable but the returned value of the function is undefined .
What does it mean when it says that: the function will change the global ‘sum’ variable, but the returned value of the function is ‘undefined’ ?
Is it because the sum variable is not declared inside of the function?
You’ll see undefined because the function did not define a return value.
However. If you print sum in between calls to addSum, you’ll see that the global variable’s value keeps changing.
Hello @Honeybee! Functions return undefined by default. They only return other values when you include a return statement in it. Regardless of whether the function has a return statement, all of the code in it will run like normal (in this case, the function adds the value of num to the sum variable). Does that answer your question?
You’re welcome, I’m always glad to help! Also, I’m glad to meet someone who’s familiar with MBTI. Just out of curiosity, what’s you’re personality type?
Oh, cool. We have very similar personality types! I used to question my MBTI results because I thought that I was also an INTP myself, but to be honest, I feel that the descriptions of both personality types describe me well. I’m always glad to meet a like-minded person.