Passing Arguments and Return Values

There are two ways of passing arguments to a function, By value and By reference.

  1. 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.
  2. 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;
}


Output:

Function Returning Value

A 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




// Auther: Nikunj Sonigara
 
#include <stdio.h>
 
// Function declaration
int square(int x);
 
// Function definition
int square(int x)
{
    return x * x; // return the square of x
}
 
int main()
{
    // Function call
    int a = 5;
    int b = square(a);
 
    printf("The square of %d is %d\n", a, b);
 
    return 0;
}


Output:

Function Not Returning Value

Some 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++




// Auther: Nikunj Sonigara
 
#include <stdio.h>
 
// Function declaration
void printHello(void);
 
// Function definition
void printHello(void)
{
    printf("Hello, world!\n");
    return; // optional, as it's not needed here
}
 
int main()
{
    // Function call
    printHello(); // prints "Hello, world!"
 
    return 0;
}


Output:

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.

Similar Reads

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....

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....

Passing Arguments and Return Values

There are two ways of passing arguments to a function, By value and By reference....

Differentiating Between Functions and Methods

...

Using Built-in Functions from the Objective-C Foundation Framework

...