Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 26, 2012, 03:34 PM   #1
Woodshark69
macrumors newbie
 
Join Date: Dec 2012
In need of help with a few beginner definitions!

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!
Woodshark69 is offline   0 Reply With Quote
Old Dec 26, 2012, 04:32 PM   #2
960design
macrumors 6502a
 
Join Date: Apr 2012
Location: Destin, FL
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
__________________
TI-99/4A, tape cassette, 12" B&W Zenith
960design is offline   0 Reply With Quote
Old Dec 26, 2012, 05:51 PM   #3
Woodshark69
Thread Starter
macrumors newbie
 
Join Date: Dec 2012
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?
Woodshark69 is offline   0 Reply With Quote
Old Dec 26, 2012, 05:55 PM   #4
grapes911
Demi-God (Moderator emeritus)
 
grapes911's Avatar
 
Join Date: Jul 2003
Location: Citizens Bank Park
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."
__________________
grapes911 is offline   0 Reply With Quote
Old Dec 26, 2012, 05:58 PM   #5
Woodshark69
Thread Starter
macrumors newbie
 
Join Date: Dec 2012
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)
Woodshark69 is offline   0 Reply With Quote
Old Dec 26, 2012, 06:10 PM   #6
ChristianJapan
macrumors Demi-God
 
ChristianJapan's Avatar
 
Join Date: May 2010
Location: 日本
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.
__________________
Member of MacRumors.com Folding@Home Team (#3446) & developer of the F@H Mobile Monitoring app.
ChristianJapan is offline   0 Reply With Quote
Old Dec 26, 2012, 06:41 PM   #7
grapes911
Demi-God (Moderator emeritus)
 
grapes911's Avatar
 
Join Date: Jul 2003
Location: Citizens Bank Park
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.
__________________
grapes911 is offline   0 Reply With Quote
Old Dec 26, 2012, 07:15 PM   #8
Ap0ks
macrumors regular
 
Join Date: Aug 2008
Location: Cambridge, UK
Quote:
Originally Posted by Woodshark69 View Post
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?
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).

Last edited by Ap0ks; Dec 26, 2012 at 07:29 PM. Reason: Edited to include a return function example
Ap0ks is offline   0 Reply With Quote
Old Dec 26, 2012, 07:51 PM   #9
Woodshark69
Thread Starter
macrumors newbie
 
Join Date: Dec 2012
I agree with both of you I think I do need a simpler book. Any suggestions?
Woodshark69 is offline   0 Reply With Quote
Old Dec 26, 2012, 08:13 PM   #10
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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.
ArtOfWarfare is offline   0 Reply With Quote
Old Dec 27, 2012, 08:11 AM   #11
Ap0ks
macrumors regular
 
Join Date: Aug 2008
Location: Cambridge, UK
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.
Ap0ks is offline   0 Reply With Quote
Old Dec 27, 2012, 08:59 AM   #12
Dreamspinner
macrumors member
 
Join Date: Dec 2012
Quote:
Originally Posted by Woodshark69 View Post
I agree with both of you I think I do need a simpler book. Any suggestions?
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.
Dreamspinner is offline   0 Reply With Quote
Old Dec 27, 2012, 01:25 PM   #13
klaxamazoo
macrumors 6502
 
Join Date: Sep 2006
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.
klaxamazoo is offline   0 Reply With Quote
Old Dec 27, 2012, 03:09 PM   #14
larswik
macrumors 65816
 
Join Date: Sep 2006
Quote:
Originally Posted by Woodshark69 View Post
I agree with both of you I think I do need a simpler book. Any suggestions?
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.
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Apple Enforces Adobe Flash Player Security Upgrade with Updated Malware Definitions MacRumors Mac Blog Discussion 51 Feb 15, 2014 12:04 PM
Grapher: How to revise definitions ritachengl Mac Applications and Mac App Store 1 Jun 26, 2013 07:44 PM
Apple Updates OS X Anti-Malware Definitions to Block 'Yontoo' Adware MacRumors MacRumors.com News Discussion 66 Mar 26, 2013 09:22 AM
Apple Quickly Updates Malware Definitions to Detect New SMS Scam Trojan MacRumors MacRumors.com News Discussion 94 Dec 26, 2012 03:39 PM
Upgraded apache from 1.3 to 2.2 now server ignores site definitions max•• Mac OS X Server, Xserve, and Networking 2 Nov 30, 2012 11:56 AM

Forum Jump

All times are GMT -5. The time now is 01:44 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC