![]() |
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for the OS X and iOS operating systems and their respective application programming interfaces (APIs), Cocoa and Cocoa Touch. One of the features of Objective-C is that it supports functions, which are named blocks of code that can be called upon to perform a specific task. Functions can be provided with data on which to perform the task and can return a result to the code that called them. Functions can help to organize the code, avoid repetition, and improve readability and maintainability. Defining and Declaring a FunctionA function definition in Objective-C consists of two parts: a function header and a function body. The function header specifies the name, return type, and parameters of the function. The function body contains a collection of statements that define what the function does. Syntax:
Example: Below code defines a function called max that takes two integers as parameters and returns the maximum between them:
A function declaration tells the compiler about the name, return type, and parameters of a function. The actual body of the function can be defined separately. A function declaration has the same syntax as the function header, followed by a semicolon. For example, the following code declares the max function:
A function declaration is also known as a function prototype. It is usually placed at the beginning of a source file or in a header file. A function declaration is optional, but it is recommended to declare a function before using it to avoid compiler errors or warnings. Calling a FunctionTo call a function, we use the function name followed by a pair of parentheses enclosing the arguments (if any). The arguments are the values that are passed to the function when it is invoked. The arguments must match the number and type of the parameters declared by the function. For example, the following code calls the max function with two arguments:
When a function is called, the control passes from the calling code to the function body. The function executes its statements and returns a value (if any) to the calling code. The control then resumes from where it left off in the calling code. Passing Arguments and Return ValuesThere are two ways of passing arguments to a function, By value and By reference.
By default, Objective-C passes arguments by value. To pass an argument by reference, we use an asterisk (*) before the parameter name in both the function declaration and definition. We also use an ampersand (&) before the argument name when calling the function. For example, the following code defines and calls a function called swap that swaps two integers by passing them by reference: ObjectiveC
Output: Function Returning ValueA function can return only one value to its caller. The return type of a function must match the type of value returned by it. To return a value from a function, we use the return keyword followed by the value or expression to be returned. For example, the following code defines and calls a function called square that returns the square of an integer: ObjectiveC
Output: Function Not Returning ValueSome functions do not return any value to their caller. In this case, the return type of the function is the keyword void. A function with a void return type can use the return keyword without any value to exit the function. For example, the following code defines and calls a function called printHello that prints “Hello, world!” to the standard output and does not return any value: C++
Output: Differentiating Between Functions and MethodsIn Objective-C, there is a distinction between functions and methods. Functions are code blocks that are unrelated to an object or a class, just inherited from C. Methods are code blocks that are attached to a class or an instance (object) and are invoked by sending messages to them. The syntax for defining and calling functions and methods is different. Functions use the standard C syntax, as shown in the previous sections. Methods use a special Objective-C syntax, as shown in the following example:
Explanation:
For example, the following code defines and calls a method called add that takes two integers as parameters and returns their sum: ObjectiveC
Output: Note that functions and methods can have the same name as long as they have different signatures. For example, we can have both a function and a method called max, but they must have different parameter lists. Using Built-in Functions from the Objective-C Foundation FrameworkThe Objective-C foundation framework provides numerous built-in functions that our program can call. These functions perform various tasks such as memory management, string manipulation, mathematical operations, etc. To use these functions, we need to import the appropriate header files that contain their declarations. Built-in FunctionFor example, to use the sqrt function that returns the square root of a double value, we need to import the math.h header file: ObjectiveC
Output: The square root of 25 is: 5.000000 MacrosSome of these functions are actually macros, which are preprocessor directives that replace a name with a code fragment. For example, the MIN and MAX macros return the minimum and maximum between two values, respectively. To use these macros, we need to import the Foundation.h header file: ObjectiveC
Output: x: 10 |
Reffered: https://www.geeksforgeeks.org
Objective C |
Related |
---|
![]() |
|
|
|
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |