+ 1

In what order does functions work?

function factorial(n) { if (n === 0) { log('Base case reached'); return 1; } log('n - 1 is ' + (n - 1)); return n * factorial(n - 1); } log(factorial(4)); In this small chunk of code, we calculate the factorial of a number. But I have a question: The part where the code goes: return n * factorial(n-1); , does it first count the parenthesis then do the function, or does it input straight "n-1" to the function?

3rd Jan 2017, 2:37 PM
The Hunter
The Hunter - avatar
1 Resposta
+ 6
It counts it first so that it will be assigned to the arguments.
3rd Jan 2017, 3:54 PM
Valen.H. ~
Valen.H. ~ - avatar