First-Class Function: A programming language is said to have First-class functions if functions in that language are treated like other variables. So the functions can be assigned to any other variable or passed as an argument or can be returned by another function.
Example: Let us take an example to understand more about the first-class function.
Output: In the above program, functions are stored as a variable in an object.
Higher-Order Function: A function that receives another function as an argument or that returns a new function or both is called Higher-order functions. Higher-order functions are only possible because of the First-class function.
Let’s take some examples to understand better:
Example 1: Functions returning another function.
Note: We can also call the function like this also — greet(‘ABC’)(‘Welcome To GeeksForGeeks’), It will also give the same output.
Hi!! ABC, Welcome To GeeksForGeeks
Example 2: Passing Function as an argument.
Note: The function that we pass as an argument to another function is called the callback function.
Key Differences between First-Order Function and Higher-Order Function:-
First-Order Function Higher-Order Function Function are treated as a variable that can be assigned to any other variable or passed as an argument. Function receives another function as an argument or returns First-order a new function or both. The “first-class” concept only has to do with functions in programming languages. The “higher-order” concept can be applied to functions in general, like functions in the mathematical sense. The presence of the First-class function implies the presence of a higher-order function. The presence of a Higher-order function does not imply the presence of a First-order function.