This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
ios-labs-s14:basics-methods [2014/02/17 11:22] mbarboi |
ios-labs-s14:basics-methods [2014/02/26 13:02] (current) mbarboi |
||
---|---|---|---|
Line 2: | Line 2: | ||
“Method”, “message”, and “function” all (sort of) refer to the same thing in Objective-C. | “Method”, “message”, and “function” all (sort of) refer to the same thing in Objective-C. | ||
- | A method is a section of code that we can call from elsewhere in our code. | + | A method is a section of code that we can execute (call) from elsewhere in our code. |
Components to methods: | Components to methods: | ||
Line 26: | Line 26: | ||
**Definition**- writing the code that executes/is contained within the method. Known as the method body. | **Definition**- writing the code that executes/is contained within the method. Known as the method body. | ||
+ | Methods are defined in **implementation** files, or //.m// files. The form is the same as the declaration, except with curly braces instead of a semicolon which contain the method body. | ||
- | Defining a Method | + | *- (RETURNTYPE) METHODNAME:(ARGUMENTTYPE*)ARGUEMENTNAME METHODNAME2:(ARGUMENTTYPE*)ARGUEMENTNAME2 {CODE} |
- | Return Type: A method may return a value. The return_type is the data type of the value the function returns. Some methods perform the desired operations without returning a value. In this case, the return_type is the keyword void. | + | |
- | Method Name: This is the actual name of the method. The method name and the parameter list together constitute the method signature. | + | ====Invocation=== |
+ | **Invocation**- the act of running the code contained within a method. A method is invoked on a target, where the target is the object that owns the method. | ||
- | Arguments: A argument is like a placeholder. When a function is invoked, you pass a value to the argument. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the arguments of a method. Arguments are optional; that is, a method may contain no argument. | + | Consists of two parts |
+ | -Target- the target object or class that owns the method; where the method will be invoked | ||
+ | -Name- the name of the method to be invoked | ||
- | Joining Argument: A joining argument is to make it easier to read and to make it clear while calling it. | + | Invoking a method takes the form: |
+ | [TARGET NAME]; | ||
+ | |||
+ | and can be nested infinitely deep: | ||
+ | [[[TARGET NAME] NAME2] NAME3]; | ||
- | Method Body: The method body contains a collection of statements that define what the method does. | + | where NAME2 and NAME3 are methods called on the objects returned by methods NAME and NAME2, respectively. |
- | + | ||
- | Declaration | + | |
- | Method declaration is required when you define a method in one source file and you call that method in another file. In such case you should declare the function at the top of the file calling the function. | + |