Xcode,Objective -c, and Cocoa

Discussion in 'Mac Programming' started by Telp, Mar 8, 2008.

  1. macrumors 68040

    Telp

    Joined:
    Feb 6, 2007
    #1
    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
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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.
     
  3. thread starter macrumors 68040

    Telp

    Joined:
    Feb 6, 2007
    #3
    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?
     
  4. macrumors member

    Joined:
    Apr 1, 2007
    Location:
    San Diego
    #4
    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)
     
  5. thread starter macrumors 68040

    Telp

    Joined:
    Feb 6, 2007
    #5
    So the argument tells the function what the function is? Am i on the right track? Maybe...
     
  6. macrumors 68040

    Joined:
    Apr 22, 2005
    #6
    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.
     
  7. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #7
    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.
     

Share This Page