Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Telp

macrumors 68040
Original poster
Feb 6, 2007
3,075
25
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

Moderator emeritus
Jun 15, 2000
7,958
7
An argument is the input to a method or a function. Another name is parameter.

For example in this method:

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

num1 and num2 are the arguments to the method.
 

Telp

macrumors 68040
Original poster
Feb 6, 2007
3,075
25
An argument is the input to a method or a function. Another name is parameter.

For example in this method:

Code:
- (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

macrumors member
Apr 1, 2007
80
0
San Diego
I made a quick example:

Lets say we wrote a function to tell us whether an integer is positive or negative:
Code:
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:

Code:
//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

macrumors 68040
Original poster
Feb 6, 2007
3,075
25
So the argument tells the function what the function is? Am i on the right track? Maybe...
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
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

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.