How to use Built-in Functions from the Objective-C Foundation Framework In C Language
The 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 Function
For 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
// Auther: Nikunj Sonigara #include <stdio.h> #include <math.h> int main() { double x = sqrt(25); // x will be 5.0 printf( "The square root of 25 is: %lf\n" , x); return 0; } |
Output:
The square root of 25 is: 5.000000
Macros
Some 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
// Auther: Nikunj Sonigara #import <Foundation/Foundation.h> int main() { int x = MIN(10, 20); // x will be 10 int y = MAX(10, 20); // y will be 20 NSLog ( @"x: %d" , x); NSLog ( @"y: %d" , y); return 0; } |
Output:
x: 10
y: 20
Functions in Objective-C
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.
Contact Us