Would like to understand this code 100%

Discussion in 'iOS Programming' started by Tander, Aug 21, 2012.

  1. Tander macrumors 6502a


    Oct 21, 2011
    Johannesburg, South Africa
    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?

    #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);
        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. iHutch105, Aug 21, 2012
    Last edited: Aug 21, 2012

    iHutch105 macrumors member

    Aug 8, 2011
    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.
  3. larswik macrumors 68000

    Sep 8, 2006
    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.
  4. Tander thread starter macrumors 6502a


    Oct 21, 2011
    Johannesburg, South Africa
    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
  5. phr0ze macrumors 6502a

    Jun 14, 2012
    Columbia, MD
    Little bits of detail like this will make your journey so painful. Pay attention to every character.
  6. balamw Moderator


    Staff Member

    Aug 16, 2005
    New England
    @Tander: In case you didn't catch it. phr0ze means that you replaced sing with sign

  7. Tander thread starter macrumors 6502a


    Oct 21, 2011
    Johannesburg, South Africa

Share This Page