Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts

    advancing i++ in different functions

    Hi All,

    In a code where the i++ within a function is used it will do its job within that local function.

    Now if for whatever reason there were need for a second function, how do you make the second function execute the first functions i++.

    As I understand it the i++ is local to the function it is in so the function two cannot execute the i++ for function one.

    Code:
    function one()
    {
    do whatever
    
    but the i++ which would normally be here is NOT.
    }
    
    
    function two()
    {
    do something else and when done execute function one's....  i++
    
    }
    Martin.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,366
    Thanks
    11
    Thanked 590 Times in 571 Posts
    if i is not private, but closed by both functions it will work.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #3
    New Coder
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    8
    Thanked 1 Time in 1 Post
    I think you are going for something like this:
    Code:
    function fullfunction(){
    var i = 0;
    function one(){
        dosomething(i);
    }
    
    function two(){
        dosomething1(i);
        i++;
    }
    
    }
    Is that what you are going for? If you want to loop through the function or call it over again, just make sure to call the full function with the i not private.

  • #4
    New Coder
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    8
    Thanked 1 Time in 1 Post
    I think you are going for something like this:
    Code:
    function fullfunction(){
    var i = 0;
    function one(){
        dosomething(i);
    }
    
    function two(){
        dosomething1(i);
        i++;
    }
    
    }
    Is that what you are going for? If you want to loop through the function or call it over again, just make sure to call the full function with the i not private.

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks for the suggestions, I did have the i++ in the wrong place. Just a small move so it fell within the main function and all worked as wanted.

    Martin.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •