In need of help with a few beginner definitions!

Discussion in 'Mac Programming' started by Woodshark69, Dec 26, 2012.

  1. macrumors newbie

    Joined:
    Dec 12, 2012
    #1
    I have recently purchased the book Objective-C Programming: Big Nerd Ranch. Before I go on I am not trying to discriminate this book. It is a great book. I just need help with a few of the vocabulary words before I move on. It says that a variable is a place where one piece of data can go. Can someone explain a little more. When I think of variable I think that it means that if you put 100 in as an integer variable the human will jump 100 somethings. Obviously I am a little lost. Any help is great help. Thanks guys!
     
  2. macrumors 65816

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
    #2
    Sounds like you have it exactly right:
    A variable is a human readable storage location, think of URLs, no one wants to hear something like: "Please visit us at 192.168.1.1". It's easier to remember: "Please visit us at yummywildwings.com"

    It is so much easier to remember that you stored, in your case, the integer, 100 at bobsbouncer. Or:
    int bobsbouncer = 100;

    The variable is just that, bobsbouncer could be changed to 50 or 23.

    Much easier/clearer than old school:
    num dw 100
     
  3. thread starter macrumors newbie

    Joined:
    Dec 12, 2012
    #3
    Okay so maybe I am on the right track. I have another question though. In the book the author uses void a lot of times and I don't understand what this does. For example he uses. Void showcooktimeforturkey(int pounds); Totally off topic but just to make sure that I understand this the int in this is saying that the number of pounds is going to have to be an integer variable also known as a number without a decimal that stores data. Nothing to do with the last sentence but what would the int variable be holding?
     
  4. Moderator emeritus

    grapes911

    Joined:
    Jul 28, 2003
    Location:
    Citizens Bank Park
    #4
    showcooktimeforturkey is a function. It takes in an integer called pounds. It does some calculation on that integer and most likely displays the time it takes to cook a turkey based on the number of pounds passed in to it. It does not return any value, thus it returns "void."
     
  5. thread starter macrumors newbie

    Joined:
    Dec 12, 2012
    #5
    But wouldn't the time it takes to cook the turkey be the value? ( I edited my last statement just after you posted to me)
     
  6. macrumors demi-god

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #6
    The name for showcooktimeforturkey imply more a function to visualize a time which would not necessary require a return value.

    If you would have a function like calulatecooktimeforturkey it would be different; that one would most likely return a floating number.

    But that's exactly the challenge with programming: choose good names for functions and variables so it makes sense after years to read source code.
     
  7. Moderator emeritus

    grapes911

    Joined:
    Jul 28, 2003
    Location:
    Citizens Bank Park
    #7
    I'm not familiar with the book you are reading but you may want to find a slightly less advanced book. Maybe one that focuses more on programming basics that will explain all this. This is pretty basic stuff. If you don't really understand it well, the rest won't make much sense.
     
  8. Ap0ks, Dec 26, 2012
    Last edited: Dec 26, 2012

    macrumors regular

    Joined:
    Aug 12, 2008
    Location:
    Cambridge, UK
    #8
    int pounds is the functions argument, that is to say it expects to be passed an integer value when the function is called.

    For example here's some pseudocode (code in English, not a programming language - you'll need to make changes to make it work) showing where the number could come from:
    Code:
    // Let's say we're cooking a 7lb turkey
    int weightOfTurkey = 7;
    
    // Now let's call our function
    showCookTimeForTurkey(weightOfTurkey);
    
    // This calls the 2nd function and will give us a variable called time containing the value 14
    int time = getCookTimeForTurkey(weightOfTurkey);
    
    // non-returning function
    void showCookTimeForTurkey(int pounds){
      // Assuming it's 2 hours per lb, let's take the passed in value and * it by 2  
      cookTime = pounds * 2;
    
      Print "A " + pounds + "lb(s) turkey will take " + cookTime + " hours";
    }
    
    // Return function example
    int getCookTimeForTurkey(int pounds){
      // we need to return an int in this function, so return the argument * 2  
      return pounds * 2
    }
    
    Result:
    A 7lb(s) turkey will take 14 hours
    
    You may want to have the program ask the user what the weight of the Turkey is, in which case you'd obtain the number from the user before calling the function with the number they provided.

    Depending on the language used, if you try to call the function with a string you'll get a warning message that the function doesn't exist or a runtime error.

    Also as grapes911 has pointed out, you may want to start with a book that covers the basics first such as Kochan's Programming in Objective-C. I have an earlier edition than the latest and it covers a lot more of the basics and then builds upon it rather than needing you to have programming experience (which i found to be the case in one of the Hillegass books).
     
  9. thread starter macrumors newbie

    Joined:
    Dec 12, 2012
    #9
    I agree with both of you I think I do need a simpler book. Any suggestions?
     
  10. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #10
    As examples:

    Code:
    void doSomething(int number)
    {
        for (int i = 0; I < number; i++)
        {
            printf("Hi");
        }
    }
    
    int multiplyByTwo(int number)
    {
        return number*2;
    }
    
    int main(int argc, char* argv[])
    {
        int newNumber = multiplyByTwo(3);
        doSomething(newNumber);
        return 0; // Indicates that the program finished without errors.
    }
    multiplyByTwo returns a variable of type integer. Thus, in my main function I can say
    Code:
    int newNumber = multiplyByTwo(3);
    and newNumber will be equal to the returned result, 6 in this case.

    On the other hand, doSomething has a return type of void, meaning it returns nothing. doSomething is a function that takes an int, goes and does something with it, but doesn't return anything back to main() when it's done. That doesn't mean it's useless though - it prints "Hi" as many times as its told to in the number it takes as an argument.

    I could combine the first two statements of main() to be just:

    Code:
    doSomething(multiplyByTwo(3));
    multiplyByTwo(3) returns 6 and instantly passes it as an argument to doSomething(), which will then print "Hi" six times.

    I hope that's helpful.

    I recommend Learn C The Hard Way, a free ebook. Google it.
     
  11. macrumors regular

    Joined:
    Aug 12, 2008
    Location:
    Cambridge, UK
    #11
    Apart from my Kochan's Programming in Objective-C recommendation and ArtofWarfare's Learn C the Hard Way you may also get some use from Become an Xcoder (the version of Xcode they use is out of date but the first few chapters is still good for basic Obj-C programming.
     
  12. macrumors member

    Joined:
    Dec 17, 2012
    #12
    The Big Nerd Ranch books are excellent, but they're maybe not for beginners. . In addition to the one you have there's "Cocoa Programming For Mac OS X". I too a course with one of the authors. It was 6 months worth in a week.

    A bit more basic one I liked was "Cocoa and Objective-C" by O'Reilly.

    A concept that a lot of people miss is that programming is language agnostic, that is there are concepts that apply no matter what the language.

    It's a fun, satisfying journey, not a quick one.
     
  13. macrumors 6502

    Joined:
    Sep 8, 2006
    #13
    I second Kochan's Programming in Objective-C.

    Programming in Objective-C is a great book and you can't go wrong with it; however, if you don't have any programming experience at all it might be worthwhile to start with Kochan's Programming in C. It spends a lot more time introducing and explaining concepts such as variables, pointers, etc. This would provide a good foundation for learning Objective-C.

    As many will tell you, the order that you learn them doesn't make a big difference, but I did find going through both books to be extremely valuable.
     
  14. macrumors 68000

    Joined:
    Sep 8, 2006
    #14
    Learning to program is not a hot dog eating contest. Eat at a good pace, enjoy your meal and let it digest.

    I started objective c years ago and got lost really fast. So before you quit and say programming is to hard step back so something like C. Don't worry about making pretty GUI's or buttons and that fun stuff, learn the basics.

    The book "Learn C on the Mac" is how I got going when I stepped back. I spent 3-4 months reading this book and redoing the tutorials my own way to see what was happening with the code. It is all console based so you see no graphics, it's just the basics to teach you how things function.

    Make sure you grasp the concepts and then you can write your own little games or things before you move on. For someone who knows nothing about programming understanding just variables can be hard to grasp.

    When you are writing your own little C programs then step back up to Objective C and after that step in to Cocoa for the graphics part.

    Just so you know.... I spent a whole year learning C and objective C before I ever even built a user interface. It was all console based. When you get lost in the book this is a great forum to help you get it.
     

Share This Page