Higher-Order Programming: Behind the Scenes!
Welcome to Higher-Order Programming!
Hi, Welcome! In this section we are going to dive into Higher-Order Programming. You will learn how to pass functions as parameters to other functions and how to return functions from functions. Let’s get started!
In the diagram below you can first see two of the functions we will be working with. These are regular functions
f
and g
that perform a simple operation on their parameter.- In the purple rectangle you can find an example of passing functions as arguments to other functions. In this case we pass the functions
f
andg
to the functionh
. - In the blue rectangle you can find an example of returning functions from functions.
Let’s look at each one in more detail! : )
Passing Functions as Arguments
The function applyFunctions
takes three parameters (f
, g
and p
). f
and g
are functions. We can determine that by looking at the body of the function. See how we call the function f
with f(p)
and how we call function g with g(p)
? These function calls will be executed and replaced by the values returned.
NOTE: In a real-world scenario, the fact that f
and g
are functions would be noted in the function's documentation.
NOTE: We will be working with the functions multiplyBy3
and add5
(I’ve added a descriptive name for the functions)
This is very exciting; an entirely new world of possibilities has emerged!
applyFunctions
takes three parameters (f
, g
and p
). f
and g
are functions. We can determine that by looking at the body of the function. See how we call the function f
with f(p)
and how we call function g with g(p)
? These function calls will be executed and replaced by the values returned.f
and g
are functions would be noted in the function's documentation.multiplyBy3
and add5
(I’ve added a descriptive name for the functions)Function Execution: Behind the Scenes!
We call the function
applyFunctions
with the name we assigned to the functions that will be passed as arguments. f
will be replaced by multiplyBy3
and g
will be replaced by add5
. You can call these functions inside the body of the function. The third parameter will be 3.- Replacing them in the body of the function for illustration purposes, we can see that the values returned will be 9 and 8 after executing each individual function call.
The tuple returned will be
(9, 8)
Returning Functions!
Returning functions from functions works exactly as if we were returning a value, but we must return the name we assigned to the function.
NOTE: In this case, since we are returning more than one function, the value returned will be a tuple.
How to call these functions
To call the functions returned we must access the corresponding index in the tuple. If the tuple is stored in the variable
var1
, then to access the function multiplyBy3
we must access index 0 and to access the function add5
we must access index 1.
As you can see in the diagram below, after accessing these functions we can call them with an argument and they will return the value we expect from executing each function.
Nice and well-thought out explanation! Good use of pictures visualizing the flow of HOP. Thank you!
ReplyDeleteThis comment has been removed by the author.
Delete