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 Function
A 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:
return_type function_name(parameter_list)
{
// body of the function
}
Example:
Below code defines a function called max that takes two integers as parameters and returns the maximum between them:
int max(int num1, int num2)
{
int result;
if (num1 > num2)
{
result = num1;
}
else
{
result = num2;
}
return result;
}
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:
int max(int num1, int num2);
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 Function
To 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:
int a = 10;
int b = 20;
int c = max(a, b); // c will be assigned 20
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 Values
There are two ways of passing arguments to a function, By value and By reference.
- Passing by value means that a copy of the argument is passed to the function, so any changes made to the argument within the function do not affect the original value.
- Passing by reference means that a pointer to the argument is passed to the function, so any changes made to the argument within the function affect the original value.
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
// Objective-C program to pass arguments
// and return values
#include <stdio.h>
// Function declaration
void swap(int *x, int *y);
// Function definition
void swap(int *x, int *y)
{
int temp;
// temp gets the value pointed by x
temp = *x;
// x gets the value pointed by y
*x = *y;
// y gets the value stored in temp
*y = temp;
}
// Driver code
int main()
{
// Function call
int a = 10;
int b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}