Javatpoint Logo
Javatpoint Logo

Higher-Order Functions

A higher-Order function contains one or more functions as an argument and returns the function as a result. Higher-Order function in PHP

Below example shows how to write the higher order function in PHP. PHP is an object-oriented programming language.

The result of the above program will look like, as shown below:

13

Higher-Order Function in Python

A function will be called a Higher Order Function when it contains the other function in the form of the parameter and returns it as an output. We will call the Higher-Order Function to those function which works with the other function. Python also supports the Higher-Order Function.

Properties of the Higher-Order Function

The properties of the Higher-Order Function are as shown below:

  1. The function will work as an instance of the object type.
  2. In the Higher-Order Function, we can easily store the function in the form of the variable.
  3. In the Higher-Order Function, a function can contain another function as a parameter.
  4. In the higher-order function, a function can also return the function.
  5. We can store these functions in the data structure in the hash table, lists etc.

Functions as object

In Python, programming language function can be assigned as a variable. Here function will not be called; instead, the reference of the function will be created.

For this, we will write the below program:

The output of the above program will look like, as shown below:

HELLO
HELLO

In the above example, a function object referenced by a call creates a second name which is, yell.

Pass function as an argument to the other function

Functions are known as Python; therefore, we can pass them as an argument to other functions. The below example shows us how we can create the greet function, which contains the function as an argument.

The output of the above program will look like, as shown below:

FUNCTION CREATED\ AND PASSED AS AN ARGUMENT.
 Function created \ and passed as an argument

Returning Function

Functions are known as objects; we can return a function from another function. In the below example, the create_address function returns the address function.

The output of the above program will look like, as shown below:

25

Decorators

For the higher-order functions, decorators are used in Python. We can modify the behavior of the class or the function by using the decorators. The decorator wraps another function to extend the wrapped function's behavior without modifying the function. Decorators contain the functions as an argument into another function and then called the decorators inside the wrapper function.

The below example shows us how we can write the higher order function in python.

The result of the above program will look like as shown below:

13





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA