Objective C Functions - Objective C

What is Function in Objective-C?

  • A group of statements that perform a task together is function. main() function is present in the Objective C program and other trivial programs can define additional functions.
  • The function declaration tells the compiler about a function's name, return type, and parameters. And function definition shows the actual body of the function.
  • The function in Objective C is called as method.
  • The Objective-C has many built-in methods that can be called.

For example, method appendString() to append string to another string.
A method can also be called as function or a sub-routine or a procedure, etc.

Defining a Method

The method definition in Objective-C programming language is as follows:

The method definition contains a method header and a method body in Objective-C programming language.Parts of a method are as follows:

  • Return Type: The return_type is the data type where function can return the value. Some methods may return a value and some methods perform desired operations without returning a value. In this case, the return_type is the keyword void.
  • Method Name: The method name and the parameter list together constitute the method signature. This is the actual name of the method.
  • Arguments: An argument is a placeholder. When a function is invoked, you can pass a value to the argument and this value is referred to as actual parameter or argument. Arguments are optional; that is, a method without argument is also valid. The parameter list refers to the type, order, and number of the arguments of a method.
  • Joining Argument: The joining argument is used to make it easier to read and clear while calling it.
  • Method Body: The method body consists of collection of statements that define what the method does.


The below code calls a method max().This method contain two parameters num1 and num2 and returns the maximum between the two:

Method Declarations:

The declaration of a method tells the compiler about a function name and how to call the method. The actual body of the function can be defined separately.
The method declaration contains the following parts:

The method declaration of the function max() is shown below:

When method is defined in one source file and calling that method in another file then Method declaration is required. For this you should declare the function at the top of the file calling the function.

Calling a method

  • We have to give a definition of what the function has to do while creating a Objective-C method. To use this method call that function to perform the defined task.
  • When a function is called the program control is transferred to the called method.
  • A defined task is performed by this called method.
  • And when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.
  • We need to pass the required parameters along with method name to call a method and if method returns a value, then you can store returned value.


On compiling the above code by keeping the max() function along with main() function the following result is produced:

Function Arguments

  • The variables must be declared by the function to accept the values of the arguments and to use arguments. These variables are called the formal parameters of the function.
  • The formal parameters can be created upon entry into the function and destroyed upon exit. And this can also behave like other local variables inside the function.

While calling a function, the arguments can be passed to a function in two ways:

Call Type
Call by value
In this method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
Call by reference
In this method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

By default, call by value is used to pass arguments in Objective-C.
The code within a function cannot alter the arguments used to call the function. In above example while calling max() function used the same method.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Objective C Topics