7 Replies - 292 Views - Last Post: 26 October 2017 - 11:37 AM

#1 bythckr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 17

Do the variables value rollback when function returns null?

Posted 25 October 2017 - 09:05 PM

I am learning recursion from Eloquent JS and I have a doubt about the behaviour of variables. I tried to understand the code by visualizing it.

What I am unable to understand is:

return find(current + 5, "(" + history + " + 5)") ||
find(current * 3, "(" + history + " * 3)");


At step 26, the second find() also returns null. After which the var ** current** had value of 63 became 21 in step 27.

It seems like the variable value reverted to the previous value of 21 at step 22 as is the value of var history.

That behaviour seems bizarre. What happens when return gets null from both the options in ||?

Is This A Good Question/Topic? 0
  • +

Replies To: Do the variables value rollback when function returns null?

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,469
  • Joined: 08-August 08

Re: Do the variables value rollback when function returns null?

Posted 26 October 2017 - 04:53 AM

What do you think this should return?

function test() {
    return null || null;
}
console.log(test());


Was This Post Helpful? 0
  • +
  • -

#3 bythckr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 17

Re: Do the variables value rollback when function returns null?

Posted 26 October 2017 - 05:08 AM

View PostCTphpnwb, on 26 October 2017 - 04:53 AM, said:

What do you think this should return?

function test() {
    return null || null;
}
console.log(test());



null.

What I don't understand is how the value that got incremented rolled back.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,052
  • Joined: 08-June 10

Re: Do the variables value rollback when function returns null?

Posted 26 October 2017 - 07:26 AM

View Postbythckr, on 26 October 2017 - 02:08 PM, said:

What I don't understand is how the value that got incremented rolled back.

No variable in that whole script ever gets updated.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,052
  • Joined: 08-June 10

Re: Do the variables value rollback when function returns null?

Posted 26 October 2017 - 07:39 AM

To be more precise, the variables current and history are local to the function, so each function call uses its own scope.

when step 26 finally returns null, all the memory (i.e. the variables) that was used while repeatedly calling find() is garbage collected. Then the || operator kicks in setting off the next branch of trials for a solution (starting with *3 instead of +5).
Was This Post Helpful? 1
  • +
  • -

#6 bythckr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 17

Re: Do the variables value rollback when function returns null?

Posted 26 October 2017 - 09:24 AM

That makes perfect sense. Thanks a lot.

Recursion within a function is a rather confusing topic. Does this have a real life application?

This post has been edited by Dormilich: 27 October 2017 - 05:16 AM

Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1829
  • View blog
  • Posts: 5,758
  • Joined: 15-January 14

Re: Do the variables value rollback when function returns null?

Posted 26 October 2017 - 10:34 AM

Of course, there are all kinds of uses for recursion. The most common I can think of is if you need to traverse a tree structure, like a file/directory tree, or any organization where a parent element could have one or more children. Maybe you have a certain element and want to know what all of its ancestors are, or you want to know what all of the descendants are. You don't know how many levels it might go so you can't just loop. Recursion works well there (although there are also ways to save those relationships in a database which don't require recursion). Recursion is also pretty common in certain math functions, that's how you calculate a factorial for example.
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,481
  • Joined: 13-June 14

Re: Do the variables value rollback when function returns null?

Posted 26 October 2017 - 11:37 AM

Recursion is used when you have a problem that can be expressed in terms of subproblems that are smaller and smaller versions of the original problem, until you get to a point where you know what the answer is directly. To use one of ArtificialSoldier's exanples - when you traverse a tree, you start at the top and if the node there isn't the one you want, well, you move down a level and you're starting at the top of another, smaller tree. You keep going and the tree you're traversing gets smaller until you find what you want, or you hit the very bottom (a leaf node). Quicksort is also recursive and recursion is used a lot in functional programming, for e.g. performing a reduce.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1