How to use Functions returned by Functions: BEHIND THE SCENES!

Welcome! : )


Hi! We will discuss an amazing tool, How to use Functions returned by Functions. Let's get started!


Code:


  • This is the code we will be working with. We have an add(x, y) function and a multiply(x, y) function that perform their corresponding operation on their parameters and return the result.
  • The function where the magic starts is determineAddOrMultiply(a). This function takes in an argument and if that argument is greater than 5 it returns the FUNCTION add. Else, if the argument is equal to or less than 5 it returns the FUNCTION multiply.

Note: I’m emphasizing that we are returning Functions because, as you can see in the diagram above, we are not using parentheses after the name of the function and therefore we are not calling the function. By using this syntax we are referring to the function itself, a reference to the entire structure that the function represents.


We have two options!


If we want to use a function returned by another function, we have two options. We can either use it immediately or we can store it in a variable for later use.
Let’s take a look at these in more detail:

Option 1: Execute immediately


If we want to get the function returned by another function and call the function returned immediately, we can use this syntax:
functionThatReturnsAFunction(arguments_for_this_function)(arguments_for_the_function_returned)
Take a look at this diagram, you'll find its description right below:


Two sets of parentheses?


I know you must be wondering why we have two sets of parentheses next to each other. We learned that functions only use one set of parentheses for their arguments, and that is completely correct, but let’s take a closer look at what happens behind the scenes, illustrated in the diagram above:
When you call the function that returns another function, such as determineAddOrMultiply(), this function call will return a function. This new function returned is going to take the place of the entire initial function call.
To illustrate this, let’s take a look at this example:
  • We call determineAddOrMultiply(6)(5, 10). Since we are passing an argument to determineAddOrMultiply that is greater than 5, the function add is returned.
  • You can imagine that this function "takes the place" of determineAddOrMultiply(6). This way, we can visualize the role of the second set of parentheses. They are the arguments for the function returned by the first function we called.
  • The add function will be executed immediately with the arguments we pass in. The value returned by calling add will be the final value returned.

In the diagram below we have another function call to determineAddOrMultiply() but this time it returns the function multiply.

OPTION 2: Store function returned in a variable


  • If you don’t want to execute the function returned immediately, you can store it in a variable for later use.
  • To do this, you call the function that returns a function and store that result on a variable. In this case, I used addFunction as the variable name.
  • This variable will hold a reference to the entire function structure. This means that you can “call” the variable as if you were calling a function, using parentheses and passing parameters at a later point in your code. Check the diagram below for an illustration of this process.

Note: You can check that the value store in this variable is a function by using the type() function and passing the variable as the only parameter.



In the diagram below, the function returned is multiply.



Hope you found this tutorial helpful!
If you have any questions, please do not hesitate to ask them on the forums or right below this post. Community TAs and your classmates will be there to help you : )
Estefania.

Comments