PDA

View Full Version : Xcode,Objective -c, and Cocoa




Telp
Mar 8, 2008, 03:06 PM
Alright, hopefully this hasnt been addressed too many times. I am trying to teach myself some objectice -c and cocoa. Right now i am reading a PDF that i found in another thread here on macrumors. However, I have come to a bit of a pivvy since i can't for my life understand what an argument is. Can someone please help me. Thanks.


-Telp



kainjow
Mar 8, 2008, 03:47 PM
An argument is the input to a method or a function. Another name is parameter.

For example in this method:

- (int)addNumber:(int)num1 toNumber:(int)num2 {
return num1 + num2;
}

num1 and num2 are the arguments to the method.

Telp
Mar 8, 2008, 04:06 PM
An argument is the input to a method or a function. Another name is parameter.

For example in this method:

- (int)addNumber:(int)num1 toNumber:(int)num2 {
return num1 + num2;
}

num1 and num2 are the arguments to the method.

can you maybe explain how the computer would read that. Im still not fully getting it. Is that code saying 1 is being added to 2? 1 is the input to 2?

Theatrics
Mar 8, 2008, 04:41 PM
I made a quick example:

Lets say we wrote a function to tell us whether an integer is positive or negative:

BOOL isPositive(float theNumber)
{
if(theNumber >= 0)
{
return YES;
}
return NO;
}


(float theNumber) is an argument passed to this function when we call it from another part of the code. So we have our function, here is an example of how we could use it:


//testing 5 to see if its positive or negative. The function returns YES or NO.
if(isPositive(5))
{
NSLog(@"the number is positive");
}
else
{
NSLog(@"the number is negative");
}


Does that help? Remember you actively see what NSLog is reporting in the run log/console window. (Run > Console)

Telp
Mar 8, 2008, 05:04 PM
So the argument tells the function what the function is? Am i on the right track? Maybe...

admanimal
Mar 8, 2008, 05:13 PM
So the argument tells the function what the function is? Am i on the right track? Maybe...

The argument allows you to provide input for the function to operate on. You can view a function as a machine that takes some input and produces some output based on the input. The argument(s) are the input to the machine.

So in the above example, when the function gets called with isPositive(5), the number 5 is being given to the function as its input (i.e. argument). Therefore, in that instance of the function, theNumber will be equal to 5. The function then returns an output value based on whether theNumber is a positive number or not.

HiRez
Mar 8, 2008, 05:47 PM
Just to clarify, arguments (parameters) to functions can be anything you want them to be: numbers, strings, memory locations, images, movies, objects such as tax records, contacts, widgets, whatever. But when you call the function, the type, count, and order of the arguments has to match what you specified when you (or other programmer) created the function.