Why a function called by another function doesn't work

Hello everyone. I was playing a bit with functions when I noticed a behavior I don’t understand:


function random(arg){
  //return arg divided by two
 return arg/2
}
function moreRandom(arg){
//silly function log argument
  console.log(arg)
}

function test(c, toDo, arg){
  //c = counter, toDo = function, arg=argument => call a function a given number of times
  for(let i = 0; i < c; i++){
    toDo(arg)
  }
}

test(3, moreRandom, 'hello') // hello hello hello
test(3, random, 8) //undefined

Why the first call trough test works and does what it was told to, while the second (which should return a number) doesn’t?
Another test:


function simple_number(){
  return 10
}
function test(a, b){
  return a * 10 + b
}
//this one works
test(simple_number(), 10) //110

//this one doesn't
let count = 2
let counter;
while(count > 0){
  counter += simple_number()
  count--
}
counter //=> NaN

Can someone help me out? Please provide some material to be studies as to elucidate the matter. Thank you!

1 Like

I’m not sure on the first one, but in the second case, you didn’t initialize the counter variable with a number. If you define it with “let counter = 0”, it will return a number.

EDIT found a solution for #1
The first test is using a function that instructs the console to log what is written for each loop. In the second test, when it goes through the loop 3 times, it evaluates toDo(arg) as 4 each time. However, the test function does not actually return any value, so it has nothing to bring back to test(3, random, 8).

4 Likes

Your test function is not returning anything.
You got 3 'hellos’s in your first example because you were using console.log().
Any function without a return will by default return undefined.

3 Likes

In the first test, you’re confusing return with console logs. Also, the output you see comes only from the first function

function moreRandom(arg){
  console.log(arg)
}
function test(c, toDo, arg){
  for(let i = 0; i < c; i++){
    toDo(arg)
  }
}
test(3, moreRandom, 'hello') // hello hello hello, undefined

So first off:

  • you’re logging out hello 3 times, which is what you expect.
  • Any function without a return value, will return and log an undefined on the console,

You can try this:

function a(){}
a()

And the undefined will appear.
That explains the output. Now why the second function doesn’t prints anything?

  • Return does not mean print, it means that you can now use that value. In this way:
function a(){return 4}
let b = a(); console.log(b) //4

So return allows you to use the output of a function, and also exits the function execution (nothing after return will be executed).


For the second one, I just run console logs, and yes everytime (the loop runs twice) you get Nan.

That’s because of this operation:

let a;
let b = a+1; console.log(b) //indeed this returns Nan

You cant add 1 to undefined (a).

2 Likes

I almost didn’t sleep last night, I finally wake up and start writing a function that doesn’t return anything (the loop is unnecessary anyway since return will automatically exit the function (@anon10002461 I didn’t want to print a value, I just used console.log to test the function ) and then I try to add 1 to undefined (thanks @darren-stoll . Good morning from guys, and thanks all for the replies. (For mods: can I keep the thread open for the day, in case I’ll need to ask other things? I’m kinda trying to review things today, hopefully better than how I started :D)

we do not usually close threads here

if you want to mark a post as solution, that’s one thing but it doesn’t close the thread

Here I am with another silly one:

let count = 0
function silly(){
 count++
}
function repeat(n, toDo, arg){
while(n > 0){
  toDo(arg)
  n--
}
}
repeat(3, silly, count) //=> count = 3

How’d I write that does the same thing but accepts an argument and performs the same task with it? (I know these questions are quite silly, but I’d like to better understand, trough them, how functions work)…

if you want a global variable passed in a function changef without referencing it directly, you can’t

var count = 0;

function plusOne(n) {
  n++;
return n;
}
function repeat(n, toDo, arg){
while(n > 0){
  toDo(arg)
  n--
}
}

using your function and using this plusOne function plus count as an argument, count is not changed

if you want to do what I think you want to do you need something like…

let counter = {count: 0}

function plusOne (arg) {
  arg.count++;
}

plusOne(counter);
console.log(counter.count) // 1
1 Like

Has that something to do with the fact that the counter is an object (and we access it trough reference) while the global variable is a primitive type (and we access it trough value)?

yes, exactly

a primitive value is immutable