Would like to understand this code 100%

Discussion in 'iPhone/iPad Programming' started by Tander, Aug 21, 2012.

  1. macrumors 6502a

    Tander

    #1
    Hey guys,

    So, I have started re-reading a book for iOS programming. I already read a lot of this book - but never took the time to 100% understand every bit of code / challenge - this was a mistake. So I stopped and have restarted.

    Book: Objective-C Programming: The Big Nerd Ranch Guide
    Author: Aaron Hillegass
    Page: 98
    Chapter: 5.

    It's the Beer Song code.

    What I would like to know is:

    I know that inside of main.c - I am called the function signTheSong. I understand how that part works - what I want to know is why do I call the fuction again, within the function and how does this code appear to loop ( It counts from 99 to 0) when there is no for / while loop code present?

    Lastly - how does the code for "Putting it in the bin, % bottles in the bin" work?

    Code:
    #include <stdio.h>
    
    void singTheSong (int numberOfBottles)
    {
        if (numberOfBottles == 0){
            printf ( "There are sinply no more bottles of beer on the wall\n");
        }else {
            printf ( "%d bottles of beer on the wall, %d bottles of beer\n", numberOfBottles, numberOfBottles);
        
        int oneFewer = numberOfBottles - 1;
        printf("Take one down, pass it around, %d bottles of beer on the wall\n", oneFewer);
        
        singTheSong(oneFewer);
        printf ( "Put a bottle in the bin, %d bottles in the bin\n", numberOfBottles);
        
        }
    }
    int main(int argc, const char * argv[])
    {
    
        singTheSong(99); //This calles the singTheSong function
        
        return 0; 
        
    }
    I guess what I am trying to understand is the flow of the code - I understand it as this:

    main kicks off "signTheSong.
    signTheSong function carries out the If / else statement - does this if / else continue to execute as long as the value is true? (There are still bottles left and "numberOfBottles" is not equal to zero )

    I would appreciate any help in understanding this code. :eek:
     
  2. macrumors 68000

    Reason077

  3. Moderator

    balamw

    Staff Member

  4. iHutch105, Aug 21, 2012
    Last edited: Aug 21, 2012

    macrumors member

    #4
    It's not that it continues to execute. The main point there is that it's calling itself.

    Check out those two links above.

    EDIT: As for how the putting the bottles in the bin section works, think back a little. I seem to remember that the function stack is mentioned earlier in that book. Think about how each function would be completed before being popped from the stack.
     
  5. macrumors 65816

    #5
    You should also take a look at the indentation for easier code reading.

    I had to take a second look at the code because I thought your else only had 1 line of code. It looked like this would create an infinite loop if you were calling the singTheSong function again outside of that loop. Then I noticed your close bracket.
     
  6. macrumors 6502a

    Tander

    #6
    Thanks for the help guys, much appreciated it. Getting to understand how it works now. :D

    Larswik - Yeah, I know you have told me before to use indentation - I'm getting there! :p
     
  7. macrumors 6502a

    #7
    Little bits of detail like this will make your journey so painful. Pay attention to every character.
     
  8. Moderator

    balamw

    Staff Member

    #8
    @Tander: In case you didn't catch it. phr0ze means that you replaced sing with sign

    B
     
  9. macrumors 6502a

    Tander

    #9
    Opps. :eek:

    Thanks guys
     

Share This Page